|
| 1 | +package socks |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net" |
| 6 | + "net/netip" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + E "github.com/sagernet/sing/common/exceptions" |
| 11 | + M "github.com/sagernet/sing/common/metadata" |
| 12 | + "github.com/sagernet/sing/protocol/socks/socks4" |
| 13 | + "github.com/sagernet/sing/protocol/socks/socks5" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + CommandTorResolve byte = 0xF0 |
| 18 | + CommandTorResolvePTR byte = 0xF1 |
| 19 | +) |
| 20 | + |
| 21 | +type TorResolver interface { |
| 22 | + LookupIP(ctx context.Context, host string) (netip.Addr, error) |
| 23 | + LookupPTR(ctx context.Context, addr netip.Addr) (string, error) |
| 24 | +} |
| 25 | + |
| 26 | +func handleTorSocks4(ctx context.Context, conn net.Conn, request socks4.Request, resolver TorResolver) error { |
| 27 | + switch request.Command { |
| 28 | + case CommandTorResolve: |
| 29 | + if !request.Destination.IsFqdn() { |
| 30 | + return E.New("socks4: torsocks: invalid destination") |
| 31 | + } |
| 32 | + ipAddr, err := resolver.LookupIP(ctx, request.Destination.Fqdn) |
| 33 | + if err != nil { |
| 34 | + err = socks4.WriteResponse(conn, socks4.Response{ |
| 35 | + ReplyCode: socks4.ReplyCodeRejectedOrFailed, |
| 36 | + }) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + return E.Cause(err, "socks4: torsocks: lookup failed for domain: ", request.Destination.Fqdn) |
| 41 | + } |
| 42 | + err = socks4.WriteResponse(conn, socks4.Response{ |
| 43 | + ReplyCode: socks4.ReplyCodeGranted, |
| 44 | + Destination: M.SocksaddrFrom(ipAddr, 0), |
| 45 | + }) |
| 46 | + if err != nil { |
| 47 | + return E.Cause(err, "socks4: torsocks: write response") |
| 48 | + } |
| 49 | + return nil |
| 50 | + case CommandTorResolvePTR: |
| 51 | + var ipAddr netip.Addr |
| 52 | + if request.Destination.IsIP() { |
| 53 | + ipAddr = request.Destination.Addr |
| 54 | + } else if strings.HasSuffix(request.Destination.Fqdn, ".in-addr.arpa") { |
| 55 | + ipAddr, _ = netip.ParseAddr(request.Destination.Fqdn[:len(request.Destination.Fqdn)-len(".in-addr.arpa")]) |
| 56 | + } else if strings.HasSuffix(request.Destination.Fqdn, ".ip6.arpa") { |
| 57 | + ipAddr, _ = netip.ParseAddr(strings.ReplaceAll(request.Destination.Fqdn[:len(request.Destination.Fqdn)-len(".ip6.arpa")], ".", ":")) |
| 58 | + } |
| 59 | + if !ipAddr.IsValid() { |
| 60 | + return E.New("socks4: torsocks: invalid destination") |
| 61 | + } |
| 62 | + host, err := resolver.LookupPTR(ctx, ipAddr) |
| 63 | + if err != nil { |
| 64 | + err = socks4.WriteResponse(conn, socks4.Response{ |
| 65 | + ReplyCode: socks4.ReplyCodeRejectedOrFailed, |
| 66 | + }) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + return E.Cause(err, "socks4: torsocks: lookup PTR failed for ip: ", ipAddr) |
| 71 | + } |
| 72 | + err = socks4.WriteResponse(conn, socks4.Response{ |
| 73 | + ReplyCode: socks4.ReplyCodeGranted, |
| 74 | + Destination: M.Socksaddr{ |
| 75 | + Fqdn: host, |
| 76 | + }, |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + return E.Cause(err, "socks4: torsocks: write response") |
| 80 | + } |
| 81 | + return nil |
| 82 | + default: |
| 83 | + return os.ErrInvalid |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func handleTorSocks5(ctx context.Context, conn net.Conn, request socks5.Request, resolver TorResolver) error { |
| 88 | + switch request.Command { |
| 89 | + case CommandTorResolve: |
| 90 | + if !request.Destination.IsFqdn() { |
| 91 | + return E.New("socks5: torsocks: invalid destination") |
| 92 | + } |
| 93 | + ipAddr, err := resolver.LookupIP(ctx, request.Destination.Fqdn) |
| 94 | + if err != nil { |
| 95 | + err = socks5.WriteResponse(conn, socks5.Response{ |
| 96 | + ReplyCode: socks5.ReplyCodeFailure, |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + return E.Cause(err, "socks5: torsocks: lookup failed for domain: ", request.Destination.Fqdn) |
| 102 | + } |
| 103 | + err = socks5.WriteResponse(conn, socks5.Response{ |
| 104 | + ReplyCode: socks5.ReplyCodeSuccess, |
| 105 | + Bind: M.SocksaddrFrom(ipAddr, 0), |
| 106 | + }) |
| 107 | + if err != nil { |
| 108 | + return E.Cause(err, "socks5: torsocks: write response") |
| 109 | + } |
| 110 | + return nil |
| 111 | + case CommandTorResolvePTR: |
| 112 | + var ipAddr netip.Addr |
| 113 | + if request.Destination.IsIP() { |
| 114 | + ipAddr = request.Destination.Addr |
| 115 | + } else if strings.HasSuffix(request.Destination.Fqdn, ".in-addr.arpa") { |
| 116 | + ipAddr, _ = netip.ParseAddr(request.Destination.Fqdn[:len(request.Destination.Fqdn)-len(".in-addr.arpa")]) |
| 117 | + } else if strings.HasSuffix(request.Destination.Fqdn, ".ip6.arpa") { |
| 118 | + ipAddr, _ = netip.ParseAddr(strings.ReplaceAll(request.Destination.Fqdn[:len(request.Destination.Fqdn)-len(".ip6.arpa")], ".", ":")) |
| 119 | + } |
| 120 | + if !ipAddr.IsValid() { |
| 121 | + return E.New("socks5: torsocks: invalid destination") |
| 122 | + } |
| 123 | + host, err := resolver.LookupPTR(ctx, ipAddr) |
| 124 | + if err != nil { |
| 125 | + err = socks5.WriteResponse(conn, socks5.Response{ |
| 126 | + ReplyCode: socks5.ReplyCodeFailure, |
| 127 | + }) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + return E.Cause(err, "socks5: torsocks: lookup PTR failed for ip: ", ipAddr) |
| 132 | + } |
| 133 | + err = socks5.WriteResponse(conn, socks5.Response{ |
| 134 | + ReplyCode: socks5.ReplyCodeSuccess, |
| 135 | + Bind: M.Socksaddr{ |
| 136 | + Fqdn: host, |
| 137 | + }, |
| 138 | + }) |
| 139 | + if err != nil { |
| 140 | + return E.Cause(err, "socks5: torsocks: write response") |
| 141 | + } |
| 142 | + return nil |
| 143 | + default: |
| 144 | + return os.ErrInvalid |
| 145 | + } |
| 146 | +} |
0 commit comments