Commit 4921d64e by anun

auto assign port

parent 2dd96a4b
......@@ -5,7 +5,7 @@ ADD . /src
WORKDIR /src
ENV CGO_ENABLED 0
RUN go build \
-ldflags "-X github.com/jpillora/chisel/share.BuildVersion=$(git describe --abbrev=0 --tags)" \
-ldflags "-X dev.nexpie.com/anun/chisel/share.BuildVersion=$(git describe --abbrev=0 --tags)" \
-o /tmp/bin
# run stage
FROM scratch
......
......@@ -3,7 +3,7 @@ BUILD=$(shell git rev-parse HEAD)
DIRBASE=./build
DIR=${DIRBASE}/${VERSION}/${BUILD}/bin
LDFLAGS=-ldflags "-s -w ${XBUILD} -buildid=${BUILD} -X github.com/jpillora/chisel/share.BuildVersion=${VERSION}"
LDFLAGS=-ldflags "-s -w ${XBUILD} -buildid=${BUILD} -X dev.nexpie.com/anun/chisel/share.BuildVersion=${VERSION}"
GOFILES=`go list ./...`
GOFILESNOTEST=`go list ./... | grep -v test`
......
# Chisel
[![GoDoc](https://godoc.org/github.com/jpillora/chisel?status.svg)](https://godoc.org/github.com/jpillora/chisel) [![CI](https://github.com/jpillora/chisel/workflows/CI/badge.svg)](https://github.com/jpillora/chisel/actions?workflow=CI)
[![GoDoc](https://godoc.org/dev.nexpie.com/anun/chisel?status.svg)](https://godoc.org/dev.nexpie.com/anun/chisel) [![CI](https://dev.nexpie.com/anun/chisel/workflows/CI/badge.svg)](https://dev.nexpie.com/anun/chisel/actions?workflow=CI)
Chisel is a fast TCP/UDP tunnel, transported over HTTP, secured via SSH. Single executable including both client and server. Written in Go (golang). Chisel is mainly useful for passing through firewalls, though it can also be used to provide a secure endpoint into your network.
......@@ -35,9 +35,9 @@ Chisel is a fast TCP/UDP tunnel, transported over HTTP, secured via SSH. Single
### Binaries
[![Releases](https://img.shields.io/github/release/jpillora/chisel.svg)](https://github.com/jpillora/chisel/releases) [![Releases](https://img.shields.io/github/downloads/jpillora/chisel/total.svg)](https://github.com/jpillora/chisel/releases)
[![Releases](https://img.shields.io/github/release/jpillora/chisel.svg)](https://dev.nexpie.com/anun/chisel/releases) [![Releases](https://img.shields.io/github/downloads/jpillora/chisel/total.svg)](https://dev.nexpie.com/anun/chisel/releases)
See [the latest release](https://github.com/jpillora/chisel/releases/latest) or download and install it now with `curl https://i.jpillora.com/chisel! | bash`
See [the latest release](https://dev.nexpie.com/anun/chisel/releases/latest) or download and install it now with `curl https://i.jpillora.com/chisel! | bash`
### Docker
......@@ -58,7 +58,7 @@ sudo dnf -y install chisel
### Source
```sh
$ go install github.com/jpillora/chisel@latest
$ go install dev.nexpie.com/anun/chisel@latest
```
## Demo
......@@ -99,7 +99,7 @@ $ chisel --help
client - runs chisel in client mode
Read more:
https://github.com/jpillora/chisel
https://dev.nexpie.com/anun/chisel
```
<!--/tmpl-->
......@@ -205,7 +205,7 @@ $ chisel server --help
X.Y.Z
Read more:
https://github.com/jpillora/chisel
https://dev.nexpie.com/anun/chisel
```
<!--/tmpl-->
......@@ -348,7 +348,7 @@ $ chisel client --help
X.Y.Z
Read more:
https://github.com/jpillora/chisel
https://dev.nexpie.com/anun/chisel
```
<!--/tmpl-->
......@@ -407,9 +407,9 @@ Since WebSockets support is required:
- http://golang.org/doc/code.html
- http://golang.org/doc/effective_go.html
- `github.com/jpillora/chisel/share` contains the shared package
- `github.com/jpillora/chisel/server` contains the server package
- `github.com/jpillora/chisel/client` contains the client package
- `dev.nexpie.com/anun/chisel/share` contains the shared package
- `dev.nexpie.com/anun/chisel/server` contains the server package
- `dev.nexpie.com/anun/chisel/client` contains the client package
## Changelog
......@@ -426,4 +426,4 @@ Since WebSockets support is required:
## License
[MIT](https://github.com/jpillora/chisel/blob/master/LICENSE) © Jaime Pillora
[MIT](https://dev.nexpie.com/anun/chisel/blob/master/LICENSE) © Jaime Pillora
......@@ -15,14 +15,15 @@ import (
"regexp"
"strings"
"time"
"io"
"github.com/gorilla/websocket"
chshare "github.com/jpillora/chisel/share"
"github.com/jpillora/chisel/share/ccrypto"
"github.com/jpillora/chisel/share/cio"
"github.com/jpillora/chisel/share/cnet"
"github.com/jpillora/chisel/share/settings"
"github.com/jpillora/chisel/share/tunnel"
chshare "dev.nexpie.com/anun/chisel/share"
"dev.nexpie.com/anun/chisel/share/ccrypto"
"dev.nexpie.com/anun/chisel/share/cio"
"dev.nexpie.com/anun/chisel/share/cnet"
"dev.nexpie.com/anun/chisel/share/settings"
"dev.nexpie.com/anun/chisel/share/tunnel"
"golang.org/x/crypto/ssh"
"golang.org/x/net/proxy"
......@@ -69,6 +70,16 @@ type Client struct {
tunnel *tunnel.Tunnel
}
func getPort(s string) (response []byte, error error) {
resp, err := http.Get(s)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
return body, err
}
// NewClient creates a new client instance
func NewClient(c *Config) (*Client, error) {
//apply default scheme
......@@ -104,8 +115,16 @@ func NewClient(c *Config) (*Client, error) {
server: u.String(),
tlsConfig: nil,
}
//set default log level
client.Logger.Info = true
p, e := getPort(c.Server+"/register")
client.Debugf("p = %s", p)
if e != nil {
client.Errorf("request %s/register : %s", c.Server, e)
}
//configure tls
if u.Scheme == "wss" {
tc := &tls.Config{}
......@@ -140,8 +159,12 @@ func NewClient(c *Config) (*Client, error) {
client.tlsConfig = tc
}
//validate remotes
client.Debugf("c.Remotes = %s", c.Remotes)
for _, s := range c.Remotes {
s = "R:"+string(p)+":"+s
client.Debugf("s = %s", s)
r, err := settings.DecodeRemote(s)
client.Debugf("r = %s", r)
if err != nil {
return nil, fmt.Errorf("Failed to decode remote '%s': %s", s, err)
}
......@@ -163,12 +186,14 @@ func NewClient(c *Config) (*Client, error) {
}
client.computed.Remotes = append(client.computed.Remotes, r)
}
client.Infof("Remotes# %s", client.computed.Remotes)
//outbound proxy
if p := c.Proxy; p != "" {
client.proxyURL, err = url.Parse(p)
if err != nil {
return nil, fmt.Errorf("Invalid proxy URL (%s)", err)
}
client.Debugf("client.proxyURL = %s", client.proxyURL)
}
//ssh auth and config
user, pass := settings.ParseAuth(c.Auth)
......
......@@ -10,10 +10,10 @@ import (
"github.com/gorilla/websocket"
"github.com/jpillora/backoff"
chshare "github.com/jpillora/chisel/share"
"github.com/jpillora/chisel/share/cnet"
"github.com/jpillora/chisel/share/cos"
"github.com/jpillora/chisel/share/settings"
chshare "dev.nexpie.com/anun/chisel/share"
"dev.nexpie.com/anun/chisel/share/cnet"
"dev.nexpie.com/anun/chisel/share/cos"
"dev.nexpie.com/anun/chisel/share/settings"
"golang.org/x/crypto/ssh"
)
......@@ -90,11 +90,14 @@ func (c *Client) connectionOnce(ctx context.Context) (connected bool, err error)
return false, err
}
}
//c.Infof("c.server = %s", c.server)
//c.Infof("c = %s", c)
wsConn, _, err := d.DialContext(ctx, c.server, c.config.Headers)
if err != nil {
return false, err
}
conn := cnet.NewWebSocketConn(wsConn)
// perform SSH handshake on net.Conn
c.Debugf("Handshaking...")
sshConn, chans, reqs, err := ssh.NewClientConn(conn, "", c.sshConfig)
......@@ -125,6 +128,7 @@ func (c *Client) connectionOnce(ctx context.Context) (connected bool, err error)
if len(configerr) > 0 {
return false, errors.New(string(configerr))
}
//c.Infof("settings.EncodeConfig = %s", settings.EncodeConfig(c.computed))
c.Infof("Connected (Latency %s)", time.Since(t0))
//connected, handover ssh connection for tunnel to use, and block
err = c.tunnel.BindSSH(ctx, sshConn, reqs, chans)
......
......@@ -9,7 +9,7 @@ import (
"testing"
"time"
"github.com/jpillora/chisel/share/ccrypto"
"dev.nexpie.com/anun/chisel/share/ccrypto"
"golang.org/x/crypto/ssh"
)
......
module github.com/jpillora/chisel
module dev.nexpie.com/anun/chisel
go 1.21
go 1.19
require (
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
......
......@@ -10,6 +10,8 @@ github.com/jpillora/ansi v1.0.3 h1:nn4Jzti0EmRfDxm7JtEs5LzCbNwd5sv+0aE+LdS9/ZQ=
github.com/jpillora/ansi v1.0.3/go.mod h1:D2tT+6uzJvN1nBVQILYWkIdq7zG+b5gcFN5WI/VyjMY=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
dev.nexpie.com/anun/chisel v1.9.1 h1:nGOF58+45WHlvDcq6AZu7En8nWOBCZHqj9boo5rB4qU=
dev.nexpie.com/anun/chisel v1.9.1/go.mod h1:qvgGfFR9ZhiDoYJM4IM1omX1HLbQSkZag8miP9u4SsQ=
github.com/jpillora/requestlog v1.0.0 h1:bg++eJ74T7DYL3DlIpiwknrtfdUA9oP/M4fL+PpqnyA=
github.com/jpillora/requestlog v1.0.0/go.mod h1:HTWQb7QfDc2jtHnWe2XEIEeJB7gJPnVdpNn52HXPvy8=
github.com/jpillora/sizestr v1.0.0 h1:4tr0FLxs1Mtq3TnsLDV+GYUWG7Q26a6s+tV5Zfw2ygw=
......@@ -26,6 +28,5 @@ golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
......@@ -12,12 +12,12 @@ import (
"strings"
"time"
chclient "github.com/jpillora/chisel/client"
chserver "github.com/jpillora/chisel/server"
chshare "github.com/jpillora/chisel/share"
"github.com/jpillora/chisel/share/ccrypto"
"github.com/jpillora/chisel/share/cos"
"github.com/jpillora/chisel/share/settings"
chclient "dev.nexpie.com/anun/chisel/client"
chserver "dev.nexpie.com/anun/chisel/server"
chshare "dev.nexpie.com/anun/chisel/share"
"dev.nexpie.com/anun/chisel/share/ccrypto"
"dev.nexpie.com/anun/chisel/share/cos"
"dev.nexpie.com/anun/chisel/share/settings"
)
var help = `
......@@ -30,7 +30,7 @@ var help = `
client - runs chisel in client mode
Read more:
https://github.com/jpillora/chisel
https://dev.nexpie.com/anun/chisel
`
......@@ -83,7 +83,7 @@ var commonHelp = `
` + chshare.BuildVersion + ` (` + runtime.Version() + `)
Read more:
https://github.com/jpillora/chisel
https://dev.nexpie.com/anun/chisel
`
......
......@@ -12,11 +12,11 @@ import (
"time"
"github.com/gorilla/websocket"
chshare "github.com/jpillora/chisel/share"
"github.com/jpillora/chisel/share/ccrypto"
"github.com/jpillora/chisel/share/cio"
"github.com/jpillora/chisel/share/cnet"
"github.com/jpillora/chisel/share/settings"
chshare "dev.nexpie.com/anun/chisel/share"
"dev.nexpie.com/anun/chisel/share/ccrypto"
"dev.nexpie.com/anun/chisel/share/cio"
"dev.nexpie.com/anun/chisel/share/cnet"
"dev.nexpie.com/anun/chisel/share/settings"
"github.com/jpillora/requestlog"
"golang.org/x/crypto/ssh"
)
......
......@@ -5,15 +5,39 @@ import (
"strings"
"sync/atomic"
"time"
"net"
"strconv"
chshare "github.com/jpillora/chisel/share"
"github.com/jpillora/chisel/share/cnet"
"github.com/jpillora/chisel/share/settings"
"github.com/jpillora/chisel/share/tunnel"
chshare "dev.nexpie.com/anun/chisel/share"
"dev.nexpie.com/anun/chisel/share/cnet"
"dev.nexpie.com/anun/chisel/share/settings"
"dev.nexpie.com/anun/chisel/share/tunnel"
"golang.org/x/crypto/ssh"
"golang.org/x/sync/errgroup"
)
func getTCPPort() (port int, err error) {
var a *net.TCPAddr
if a, err = net.ResolveTCPAddr("tcp", "localhost:0"); err == nil {
var l *net.TCPListener
if l, err = net.ListenTCP("tcp", a); err == nil {
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}
}
return
}
func createListener(protocol string) (l net.Listener, close func()) {
l, err := net.Listen(protocol, ":0")
if err != nil {
panic(err)
}
return l, func() {
_ = l.Close()
}
}
// handleClientHandler is the main http websocket handler for the chisel server
func (s *Server) handleClientHandler(w http.ResponseWriter, r *http.Request) {
//websockets upgrade AND has chisel prefix
......@@ -41,6 +65,14 @@ func (s *Server) handleClientHandler(w http.ResponseWriter, r *http.Request) {
case "/version":
w.Write([]byte(chshare.BuildVersion))
return
case "/register":
ltn, err := getTCPPort()
if err != nil {
w.WriteHeader(500)
w.Write([]byte("Err"))
}
w.Write([]byte(strconv.Itoa(ltn)))
return
}
//missing :O
w.WriteHeader(404)
......
......@@ -10,7 +10,7 @@ import (
"os/user"
"path/filepath"
"github.com/jpillora/chisel/share/settings"
"dev.nexpie.com/anun/chisel/share/settings"
"golang.org/x/crypto/acme/autocert"
)
......
......@@ -6,7 +6,7 @@ import (
"sync/atomic"
"time"
"github.com/jpillora/chisel/share/cio"
"dev.nexpie.com/anun/chisel/share/cio"
"github.com/jpillora/sizestr"
)
......
......@@ -3,12 +3,12 @@ package chshare
//this file exists to maintain backwards compatibility
import (
"github.com/jpillora/chisel/share/ccrypto"
"github.com/jpillora/chisel/share/cio"
"github.com/jpillora/chisel/share/cnet"
"github.com/jpillora/chisel/share/cos"
"github.com/jpillora/chisel/share/settings"
"github.com/jpillora/chisel/share/tunnel"
"dev.nexpie.com/anun/chisel/share/ccrypto"
"dev.nexpie.com/anun/chisel/share/cio"
"dev.nexpie.com/anun/chisel/share/cnet"
"dev.nexpie.com/anun/chisel/share/cos"
"dev.nexpie.com/anun/chisel/share/settings"
"dev.nexpie.com/anun/chisel/share/tunnel"
)
const (
......
......@@ -9,7 +9,7 @@ import (
"sync"
"github.com/fsnotify/fsnotify"
"github.com/jpillora/chisel/share/cio"
"dev.nexpie.com/anun/chisel/share/cio"
)
type Users struct {
......
......@@ -11,9 +11,9 @@ import (
"time"
"github.com/armon/go-socks5"
"github.com/jpillora/chisel/share/cio"
"github.com/jpillora/chisel/share/cnet"
"github.com/jpillora/chisel/share/settings"
"dev.nexpie.com/anun/chisel/share/cio"
"dev.nexpie.com/anun/chisel/share/cnet"
"dev.nexpie.com/anun/chisel/share/settings"
"golang.org/x/crypto/ssh"
"golang.org/x/sync/errgroup"
)
......
......@@ -6,8 +6,8 @@ import (
"net"
"sync"
"github.com/jpillora/chisel/share/cio"
"github.com/jpillora/chisel/share/settings"
"dev.nexpie.com/anun/chisel/share/cio"
"dev.nexpie.com/anun/chisel/share/settings"
"github.com/jpillora/sizestr"
"golang.org/x/crypto/ssh"
)
......
......@@ -11,8 +11,8 @@ import (
"sync/atomic"
"time"
"github.com/jpillora/chisel/share/cio"
"github.com/jpillora/chisel/share/settings"
"dev.nexpie.com/anun/chisel/share/cio"
"dev.nexpie.com/anun/chisel/share/settings"
"github.com/jpillora/sizestr"
"golang.org/x/crypto/ssh"
"golang.org/x/sync/errgroup"
......
......@@ -6,9 +6,9 @@ import (
"net"
"strings"
"github.com/jpillora/chisel/share/cio"
"github.com/jpillora/chisel/share/cnet"
"github.com/jpillora/chisel/share/settings"
"dev.nexpie.com/anun/chisel/share/cio"
"dev.nexpie.com/anun/chisel/share/cnet"
"dev.nexpie.com/anun/chisel/share/settings"
"github.com/jpillora/sizestr"
"golang.org/x/crypto/ssh"
)
......
......@@ -8,8 +8,8 @@ import (
"sync"
"time"
"github.com/jpillora/chisel/share/cio"
"github.com/jpillora/chisel/share/settings"
"dev.nexpie.com/anun/chisel/share/cio"
"dev.nexpie.com/anun/chisel/share/settings"
)
func (t *Tunnel) handleUDP(l *cio.Logger, rwc io.ReadWriteCloser, hostPort string) error {
......
......@@ -25,7 +25,7 @@ import (
"path"
"strconv"
"github.com/jpillora/chisel/share/cnet"
"dev.nexpie.com/anun/chisel/share/cnet"
"time"
)
......
......@@ -3,8 +3,8 @@ package e2e_test
import (
"testing"
chclient "github.com/jpillora/chisel/client"
chserver "github.com/jpillora/chisel/server"
chclient "dev.nexpie.com/anun/chisel/client"
chserver "dev.nexpie.com/anun/chisel/server"
)
//TODO tests for:
......
......@@ -3,8 +3,8 @@ package e2e_test
import (
"testing"
chclient "github.com/jpillora/chisel/client"
chserver "github.com/jpillora/chisel/server"
chclient "dev.nexpie.com/anun/chisel/client"
chserver "dev.nexpie.com/anun/chisel/server"
)
func TestBase(t *testing.T) {
......
......@@ -18,8 +18,8 @@ import (
"path"
"time"
chclient "github.com/jpillora/chisel/client"
chserver "github.com/jpillora/chisel/server"
chclient "dev.nexpie.com/anun/chisel/client"
chserver "dev.nexpie.com/anun/chisel/server"
)
type tlsConfig struct {
......
......@@ -10,8 +10,8 @@ import (
"testing"
"time"
chclient "github.com/jpillora/chisel/client"
chserver "github.com/jpillora/chisel/server"
chclient "dev.nexpie.com/anun/chisel/client"
chserver "dev.nexpie.com/anun/chisel/server"
)
const debug = true
......
......@@ -4,8 +4,8 @@ import (
"path"
"testing"
chclient "github.com/jpillora/chisel/client"
chserver "github.com/jpillora/chisel/server"
chclient "dev.nexpie.com/anun/chisel/client"
chserver "dev.nexpie.com/anun/chisel/server"
)
func TestTLS(t *testing.T) {
......
......@@ -6,8 +6,8 @@ import (
"testing"
"time"
chclient "github.com/jpillora/chisel/client"
chserver "github.com/jpillora/chisel/server"
chclient "dev.nexpie.com/anun/chisel/client"
chserver "dev.nexpie.com/anun/chisel/server"
"golang.org/x/sync/errgroup"
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment