Skip to content

Commit 0fdc693

Browse files
committed
splitting out into modules, currently not working
1 parent 03f2cab commit 0fdc693

File tree

10 files changed

+434
-90
lines changed

10 files changed

+434
-90
lines changed

chisel-forward/client/client.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package client
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strings"
7+
8+
"github.com/hashicorp/yamux"
9+
"github.com/jpillora/chisel"
10+
"golang.org/x/net/websocket"
11+
)
12+
13+
type Client struct {
14+
config *chisel.Config
15+
proxies []*ProxyServer
16+
ws *websocket.Conn
17+
session *yamux.Session
18+
}
19+
20+
func NewClient(auth, server string, args []string) *Client {
21+
22+
c := &chisel.Config{
23+
Version: chisel.Version,
24+
Auth: auth,
25+
Server: server,
26+
}
27+
28+
for _, a := range args {
29+
r, err := chisel.DecodeRemote(a)
30+
if err != nil {
31+
log.Fatalf("Remote decode failed: %s", err)
32+
}
33+
c.Remotes = append(c.Remotes, r)
34+
}
35+
36+
return &Client{config: c}
37+
}
38+
39+
func (c *Client) Start() {
40+
s, err := chisel.EncodeConfig(c.config)
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
45+
url := strings.Replace(c.config.Server, "http:", "ws:", 1)
46+
c.ws, err = websocket.Dial(url, s, "http://localhost/")
47+
if err != nil {
48+
log.Fatal(err)
49+
}
50+
51+
// Setup client side of yamux
52+
c.session, err = yamux.Client(c.ws, nil)
53+
if err != nil {
54+
log.Fatal(err)
55+
}
56+
57+
//prepare proxies
58+
for id, r := range c.config.Remotes {
59+
if err := c.setupProxy(id, r, c.session); err != nil {
60+
log.Fatal(err)
61+
}
62+
}
63+
64+
// ws.Write([]byte("hello!"))
65+
66+
fmt.Printf("Connected to %s\n", c.config.Server)
67+
c.handleData()
68+
fmt.Printf("Disconnected\n")
69+
}
70+
71+
func (c *Client) setupProxy(id int, r *chisel.Remote, session *yamux.Session) error {
72+
73+
addr := r.LocalHost + ":" + r.LocalPort
74+
75+
proxy := NewProxyServer(id, addr, session)
76+
//watch conn for writes
77+
// go func() {
78+
// for b := range conn.out {
79+
// //encode
80+
// c.ws.Write(b)
81+
// }
82+
// }()
83+
84+
go proxy.start()
85+
c.proxies = append(c.proxies, proxy)
86+
return nil
87+
}
88+
89+
func (c *Client) handleData() {
90+
91+
buff := make([]byte, 0xffff)
92+
for {
93+
n, err := c.ws.Read(buff)
94+
95+
if err != nil {
96+
break
97+
}
98+
99+
b := buff[:n]
100+
101+
fmt.Printf("%s\n", b)
102+
103+
//decode
104+
//place on proxy's read queue
105+
}
106+
}

chisel-forward/client/proxy-server.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package client
2+
3+
//proxy conn server | clients
4+
// /-> ws <-|-> ws <--> local <-
5+
// proxy <---> ws <-|-> ws <--> local <-
6+
// \-> ws <-|-> ws <--> local <-
7+
import (
8+
"encoding/binary"
9+
"log"
10+
"net"
11+
12+
"github.com/hashicorp/yamux"
13+
"github.com/jpillora/chisel"
14+
)
15+
16+
type ProxyServer struct {
17+
id int
18+
addr string
19+
session *yamux.Session
20+
}
21+
22+
func NewProxyServer(id int, addr string, session *yamux.Session) *ProxyServer {
23+
return &ProxyServer{
24+
id: id,
25+
addr: addr,
26+
session: session,
27+
}
28+
}
29+
30+
func (r *ProxyServer) start() {
31+
32+
a, err := net.ResolveTCPAddr("tcp4", r.addr)
33+
if err != nil {
34+
log.Fatal(err)
35+
}
36+
37+
l, err := net.ListenTCP("tcp4", a)
38+
if err != nil {
39+
log.Fatal(err)
40+
}
41+
42+
for {
43+
src, err := l.Accept()
44+
if err != nil {
45+
log.Println(err)
46+
return
47+
}
48+
49+
dst, err := r.session.Open()
50+
if err != nil {
51+
log.Println(err)
52+
return
53+
}
54+
55+
b := make([]byte, 2)
56+
binary.BigEndian.PutUint16(b, uint16(r.id))
57+
dst.Write(b)
58+
59+
chisel.Pipe(src, dst)
60+
}
61+
}
62+
63+
func (r *ProxyServer) Send() {
64+
65+
}
66+
67+
func (r *ProxyServer) Recieve() {
68+
69+
}

chisel-forward/main.go

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package main
22

33
import (
4-
"encoding/json"
54
"flag"
65
"fmt"
76
"log"
87
"os"
98

10-
"github.com/jpillora/chisel"
9+
"github.com/jpillora/chisel/chisel-forward/client"
1110
)
1211

1312
const help = `
14-
Usage: chisel-forward [--auth str] remote [remote] [remote]
13+
Usage: chisel-forward [--auth str] server remote [remote] [remote] ...
1514
16-
where a remote is in the form:
17-
example.com:3000 (http://0.0.0.0:3000 => http://example.com:3000)
18-
3000:google.com:80 (http://0.0.0.0:3000 => http://google.com:80)
15+
where server is the URL to the chiseld server
16+
17+
where a remote is remote connection via the server, in the form
18+
example.com:3000 (which means http://0.0.0.0:3000 => http://example.com:3000)
19+
3000:google.com:80 (which means http://0.0.0.0:3000 => http://google.com:80)
1920
2021
`
2122

@@ -27,30 +28,12 @@ func main() {
2728
}
2829
flag.Parse()
2930
args := flag.Args()
30-
31-
c := &chisel.Config{
32-
Version: chisel.Version,
33-
Auth: *auth,
34-
}
35-
36-
for _, a := range args {
37-
r, err := chisel.DecodeRemote(a)
38-
if err != nil {
39-
log.Fatalf("Remote decode failed: %s", err)
40-
}
41-
c.Remotes = append(c.Remotes, r)
42-
}
43-
44-
if len(c.Remotes) == 0 {
45-
log.Fatalf("At least one remote is required")
46-
}
47-
48-
fmt.Printf("Forwarding:\n")
49-
for i, r := range c.Remotes {
50-
fmt.Printf(" [#%d] %s:%s -> %s:%s\n", i+1, r.LocalHost, r.LocalPort, r.RemoteHost, r.RemotePort)
31+
if len(args) < 2 {
32+
log.Fatalf("A server and least one remote is required")
5133
}
5234

53-
b, _ := json.Marshal(c)
35+
server := args[0]
36+
args = args[1:]
5437

55-
fmt.Printf("%s", b)
38+
client.NewClient(*auth, server, args).Start()
5639
}

chiseld/main.go

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,61 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
5-
"io"
6-
"log"
7-
"net/http"
86
"os"
97

10-
"github.com/jpillora/chisel"
11-
"golang.org/x/net/websocket"
8+
"github.com/jpillora/chisel/chiseld/server"
129
)
1310

14-
func main() {
11+
const help = `
12+
Usage: chiseld [--host 0.0.0.0] [--port 8080] [--auth str]
1513
16-
auth := os.Getenv("AUTH")
14+
host defines the HTTP listening host – the
15+
network interface (defaults to 0.0.0.0). You
16+
may also set the HOST environment variable.
1717
18-
handshakeWS := func(h string) (*chisel.Config, error) {
19-
c, err := chisel.DecodeConfig(h)
20-
if err != nil {
21-
return nil, err
22-
}
23-
if chisel.Version != c.Version {
24-
return nil, fmt.Errorf("Version mismatch")
25-
}
26-
if auth != "" {
27-
if auth != c.Auth {
28-
return nil, fmt.Errorf("Authentication failed")
29-
}
30-
}
31-
return c, nil
32-
}
18+
port defines the HTTP listening port (defaults
19+
to 8080). You may also set the PORT environment
20+
variable.
21+
22+
auth specifies the exact authentication string
23+
the client must provide to attain access. You
24+
may also set the AUTH environment variable.
25+
26+
`
3327

34-
handleWS := websocket.Handler(func(ws *websocket.Conn) {
35-
protos := ws.Config().Protocol
36-
if len(protos) != 1 {
37-
ws.Write([]byte("Handshake invalid"))
38-
ws.Close()
39-
return
40-
}
41-
config, err := handshakeWS(protos[0])
42-
if err != nil {
43-
ws.Write([]byte("Handshake denied: " + err.Error()))
44-
ws.Close()
45-
return
46-
}
47-
fmt.Printf("%+v\n", config)
48-
io.Copy(ws, ws)
49-
})
28+
func main() {
29+
30+
hostf := flag.String("host", "", "")
31+
portf := flag.String("port", "", "")
32+
authf := flag.String("auth", "", "")
33+
flag.Usage = func() {
34+
fmt.Fprintf(os.Stderr, help)
35+
os.Exit(1)
36+
}
37+
flag.Parse()
5038

51-
handleHTTP := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52-
if r.Header.Get("Upgrade") == "websocket" {
53-
handleWS.ServeHTTP(w, r)
54-
} else {
55-
w.WriteHeader(200)
56-
w.Write([]byte("hello world\n"))
57-
}
58-
})
39+
host := *hostf
40+
if host == "" {
41+
host = os.Getenv("HOST")
42+
}
43+
if host == "" {
44+
host = "0.0.0.0"
45+
}
5946

60-
//get port
61-
port := os.Getenv("PORT")
47+
port := *portf
48+
if port == "" {
49+
port = os.Getenv("PORT")
50+
}
6251
if port == "" {
6352
port = "8080"
6453
}
65-
//listen
66-
log.Println("listening on " + port)
67-
log.Fatal(http.ListenAndServe(":"+port, handleHTTP))
54+
55+
auth := *authf
56+
if auth == "" {
57+
auth = os.Getenv("AUTH")
58+
}
59+
60+
server.NewServer(auth).Start(host, port)
6861
}

chiseld/server/endpoint.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package server
2+
3+
import (
4+
"log"
5+
"net"
6+
7+
"github.com/jpillora/chisel"
8+
)
9+
10+
type Endpoint struct {
11+
id int
12+
addr string
13+
session chan net.Conn
14+
}
15+
16+
func NewEndpoint(id int, addr string) *Endpoint {
17+
return &Endpoint{
18+
id: id,
19+
addr: addr,
20+
session: make(chan net.Conn),
21+
}
22+
}
23+
24+
func (e *Endpoint) start() {
25+
26+
laddr, _ := net.ResolveTCPAddr("tcp4", "127.0.0.1")
27+
raddr, err := net.ResolveTCPAddr("tcp4", e.addr)
28+
if err != nil {
29+
log.Fatal(err)
30+
return
31+
}
32+
33+
for src := range e.session {
34+
35+
dst, err := net.DialTCP("tcp4", laddr, raddr)
36+
if err != nil {
37+
log.Println(err)
38+
src.Close()
39+
continue
40+
}
41+
42+
chisel.Pipe(src, dst)
43+
}
44+
}

0 commit comments

Comments
 (0)