|
| 1 | +package tunnel |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/gob" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "net" |
| 9 | + |
| 10 | + "github.com/jpillora/chisel/share/cio" |
| 11 | + "github.com/meteorite/scope" |
| 12 | + "github.com/meteorite/socks5" |
| 13 | + "golang.org/x/crypto/ssh" |
| 14 | +) |
| 15 | + |
| 16 | +type socksHandler struct { |
| 17 | + p *Proxy |
| 18 | + udpLocalAddr *socks5.AddrSpec |
| 19 | + sl *log.Logger |
| 20 | + udp *socks5.SingleUDPPortAssociate |
| 21 | +} |
| 22 | + |
| 23 | +func newSocksHandler(p *Proxy, localUDPAddr *net.UDPAddr, sl *log.Logger) *socksHandler { |
| 24 | + return &socksHandler{ |
| 25 | + p: p, |
| 26 | + udpLocalAddr: &socks5.AddrSpec{ |
| 27 | + IP: localUDPAddr.IP, |
| 28 | + Port: localUDPAddr.Port, |
| 29 | + }, |
| 30 | + sl: sl, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func (h *socksHandler) OnStartServe(ctxServer socks5.ContextGo, _ net.Listener) error { |
| 35 | + h.udp = socks5.MakeSingleUDPPortAssociate(h.udpLocalAddr, h, h.sl) |
| 36 | + return h.udp.ListenAndServeUDPPort(ctxServer, "udp") |
| 37 | +} |
| 38 | + |
| 39 | +func (h *socksHandler) ErrLog() socks5.ErrorLogger { |
| 40 | + return h.sl |
| 41 | +} |
| 42 | + |
| 43 | +func (h *socksHandler) OnConnect(ctx context.Context, conn net.Conn, req *socks5.Request) error { |
| 44 | + return h.p.pipeRemote(ctx, conn, req.DestAddr.Address()+"/sot", func(dst ssh.Channel) error { |
| 45 | + code := []byte{0} |
| 46 | + _, err := dst.Read(code) |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("can't receive socks code from server: %w", err) |
| 49 | + } |
| 50 | + if code[0] != socks5.ReplySucceeded { |
| 51 | + if err = req.SendError(conn, code[0]); err != nil { |
| 52 | + return fmt.Errorf("failed to send reply to client: %w", err) |
| 53 | + } |
| 54 | + return fmt.Errorf("can't connect to destination server (code: %d)", code[0]) |
| 55 | + } |
| 56 | + if err := req.SendConnectSuccess(conn); err != nil { |
| 57 | + return fmt.Errorf("failed to send reply to client: %w", err) |
| 58 | + } |
| 59 | + return nil |
| 60 | + }) |
| 61 | +} |
| 62 | + |
| 63 | +func (h *socksHandler) OnAssociate(_ context.Context, conn net.Conn, _ *socks5.Request) error { |
| 64 | + return h.udp.OnAssociate(conn) |
| 65 | +} |
| 66 | + |
| 67 | +func (h *socksHandler) MaxUDPPacketSize() uint { |
| 68 | + return maxMTU |
| 69 | +} |
| 70 | + |
| 71 | +type socksUdpConnector struct { |
| 72 | + *cio.Logger |
| 73 | + outbound *udpChannel |
| 74 | +} |
| 75 | + |
| 76 | +func (h *socksHandler) MakeRemoteUDPConn( |
| 77 | + ctxClient socks5.ContextGo, _ socks5.ContextGo, sendBack socks5.UDPSendBack, onBroken func(), |
| 78 | +) (socks5.RemoteUDPConn, error) { |
| 79 | + sshConn := h.p.sshTun.getSSH(ctxClient.Ctx()) |
| 80 | + if sshConn == nil { |
| 81 | + return nil, fmt.Errorf("ssh-conn nil") |
| 82 | + } |
| 83 | + dstSpec := "/sou" //just "/sou" since the remote destination address is sent with each packet |
| 84 | + rwc, reqs, err := sshConn.OpenChannel("chisel", []byte(dstSpec)) |
| 85 | + if err != nil { |
| 86 | + return nil, fmt.Errorf("ssh-chan error: %w", err) |
| 87 | + } |
| 88 | + ctxClient.GoNoError(func() { ssh.DiscardRequests(reqs) }) |
| 89 | + |
| 90 | + c := &socksUdpConnector{ |
| 91 | + Logger: h.p.Logger, |
| 92 | + outbound: &udpChannel{ |
| 93 | + r: gob.NewDecoder(rwc), |
| 94 | + w: gob.NewEncoder(rwc), |
| 95 | + c: rwc, |
| 96 | + }, |
| 97 | + } |
| 98 | + ctxClient.GoNoError(func() { |
| 99 | + defer onBroken() |
| 100 | + defer scope.Closer(ctxClient.Ctx(), c.outbound.c).Close() |
| 101 | + |
| 102 | + for { |
| 103 | + //receive from channel, including source address |
| 104 | + p := udpPacket{} |
| 105 | + c.Debugf("reading next udp packet from ssh channel to remote") |
| 106 | + if err := c.outbound.decode(&p); err != nil { |
| 107 | + c.Debugf("decode error: %s", err) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + //parse source address |
| 112 | + fromAddr, err := socks5.ParseHostPort(p.Src) |
| 113 | + if err != nil { |
| 114 | + c.Debugf("error parsing received packet source spec: %s: %s", p.Src, err) |
| 115 | + continue |
| 116 | + } |
| 117 | + |
| 118 | + //write back to inbound udp |
| 119 | + err = sendBack(fromAddr, p.Payload) |
| 120 | + if err != nil { |
| 121 | + c.Debugf("send back error: %s", err) |
| 122 | + return |
| 123 | + } |
| 124 | + } |
| 125 | + }) |
| 126 | + c.Debugf("new ssh channel for udp is created") |
| 127 | + return c, nil |
| 128 | +} |
| 129 | + |
| 130 | +func (c *socksUdpConnector) Send(_ context.Context, data []byte, remoteAddr *socks5.AddrSpec) error { |
| 131 | + return c.outbound.encode(remoteAddr.Address(), data) |
| 132 | +} |
| 133 | +func (c *socksUdpConnector) Close() error { |
| 134 | + return c.outbound.c.Close() |
| 135 | +} |
0 commit comments