|
| 1 | +package outbound |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/binary" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/saba-futai/sudoku/apis" |
| 15 | + "github.com/saba-futai/sudoku/pkg/crypto" |
| 16 | + "github.com/saba-futai/sudoku/pkg/obfs/httpmask" |
| 17 | + "github.com/saba-futai/sudoku/pkg/obfs/sudoku" |
| 18 | + |
| 19 | + N "github.com/metacubex/mihomo/common/net" |
| 20 | + "github.com/metacubex/mihomo/component/dialer" |
| 21 | + "github.com/metacubex/mihomo/component/proxydialer" |
| 22 | + C "github.com/metacubex/mihomo/constant" |
| 23 | +) |
| 24 | + |
| 25 | +type Sudoku struct { |
| 26 | + *Base |
| 27 | + option *SudokuOption |
| 28 | + table *sudoku.Table |
| 29 | + baseConf apis.ProtocolConfig |
| 30 | +} |
| 31 | + |
| 32 | +type SudokuOption struct { |
| 33 | + BasicOption |
| 34 | + Name string `proxy:"name"` |
| 35 | + Server string `proxy:"server"` |
| 36 | + Port int `proxy:"port"` |
| 37 | + Key string `proxy:"key"` |
| 38 | + AEADMethod string `proxy:"aead-method,omitempty"` |
| 39 | + PaddingMin *int `proxy:"padding-min,omitempty"` |
| 40 | + PaddingMax *int `proxy:"padding-max,omitempty"` |
| 41 | + Seed string `proxy:"seed,omitempty"` |
| 42 | + TableType string `proxy:"table-type,omitempty"` // "prefer_ascii" or "prefer_entropy" |
| 43 | +} |
| 44 | + |
| 45 | +// DialContext implements C.ProxyAdapter |
| 46 | +func (s *Sudoku) DialContext(ctx context.Context, metadata *C.Metadata) (C.Conn, error) { |
| 47 | + return s.DialContextWithDialer(ctx, dialer.NewDialer(s.DialOptions()...), metadata) |
| 48 | +} |
| 49 | + |
| 50 | +// DialContextWithDialer implements C.ProxyAdapter |
| 51 | +func (s *Sudoku) DialContextWithDialer(ctx context.Context, d C.Dialer, metadata *C.Metadata) (_ C.Conn, err error) { |
| 52 | + if len(s.option.DialerProxy) > 0 { |
| 53 | + d, err = proxydialer.NewByName(s.option.DialerProxy, d) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + cfg, err := s.buildConfig(metadata) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + c, err := d.DialContext(ctx, "tcp", s.addr) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("%s connect error: %w", s.addr, err) |
| 67 | + } |
| 68 | + |
| 69 | + defer func() { |
| 70 | + safeConnClose(c, err) |
| 71 | + }() |
| 72 | + |
| 73 | + if ctx.Done() != nil { |
| 74 | + done := N.SetupContextForConn(ctx, c) |
| 75 | + defer done(&err) |
| 76 | + } |
| 77 | + |
| 78 | + c, err = s.streamConn(c, cfg) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + return NewConn(c, s), nil |
| 84 | +} |
| 85 | + |
| 86 | +// ListenPacketContext implements C.ProxyAdapter |
| 87 | +func (s *Sudoku) ListenPacketContext(ctx context.Context, metadata *C.Metadata) (C.PacketConn, error) { |
| 88 | + return nil, C.ErrNotSupport |
| 89 | +} |
| 90 | + |
| 91 | +// SupportUOT implements C.ProxyAdapter |
| 92 | +func (s *Sudoku) SupportUOT() bool { |
| 93 | + return false // Sudoku protocol only supports TCP |
| 94 | +} |
| 95 | + |
| 96 | +// SupportWithDialer implements C.ProxyAdapter |
| 97 | +func (s *Sudoku) SupportWithDialer() C.NetWork { |
| 98 | + return C.TCP |
| 99 | +} |
| 100 | + |
| 101 | +// ProxyInfo implements C.ProxyAdapter |
| 102 | +func (s *Sudoku) ProxyInfo() C.ProxyInfo { |
| 103 | + info := s.Base.ProxyInfo() |
| 104 | + info.DialerProxy = s.option.DialerProxy |
| 105 | + return info |
| 106 | +} |
| 107 | + |
| 108 | +func (s *Sudoku) buildConfig(metadata *C.Metadata) (*apis.ProtocolConfig, error) { |
| 109 | + if metadata == nil || metadata.DstPort == 0 || !metadata.Valid() { |
| 110 | + return nil, fmt.Errorf("invalid metadata for sudoku outbound") |
| 111 | + } |
| 112 | + |
| 113 | + cfg := s.baseConf |
| 114 | + cfg.TargetAddress = metadata.RemoteAddress() |
| 115 | + |
| 116 | + if err := cfg.ValidateClient(); err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + return &cfg, nil |
| 120 | +} |
| 121 | + |
| 122 | +func (s *Sudoku) streamConn(rawConn net.Conn, cfg *apis.ProtocolConfig) (_ net.Conn, err error) { |
| 123 | + if err = httpmask.WriteRandomRequestHeader(rawConn, cfg.ServerAddress); err != nil { |
| 124 | + return nil, fmt.Errorf("write http mask failed: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + obfsConn := sudoku.NewConn(rawConn, cfg.Table, cfg.PaddingMin, cfg.PaddingMax, false) |
| 128 | + cConn, err := crypto.NewAEADConn(obfsConn, cfg.Key, cfg.AEADMethod) |
| 129 | + if err != nil { |
| 130 | + return nil, fmt.Errorf("setup crypto failed: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + handshake := buildSudokuHandshakePayload(cfg.Key) |
| 134 | + if _, err = cConn.Write(handshake[:]); err != nil { |
| 135 | + cConn.Close() |
| 136 | + return nil, fmt.Errorf("send handshake failed: %w", err) |
| 137 | + } |
| 138 | + |
| 139 | + if err = writeTargetAddress(cConn, cfg.TargetAddress); err != nil { |
| 140 | + cConn.Close() |
| 141 | + return nil, fmt.Errorf("send target address failed: %w", err) |
| 142 | + } |
| 143 | + |
| 144 | + return cConn, nil |
| 145 | +} |
| 146 | + |
| 147 | +func NewSudoku(option SudokuOption) (*Sudoku, error) { |
| 148 | + if option.Server == "" { |
| 149 | + return nil, fmt.Errorf("server is required") |
| 150 | + } |
| 151 | + if option.Port <= 0 || option.Port > 65535 { |
| 152 | + return nil, fmt.Errorf("invalid port: %d", option.Port) |
| 153 | + } |
| 154 | + if option.Key == "" { |
| 155 | + return nil, fmt.Errorf("key is required") |
| 156 | + } |
| 157 | + |
| 158 | + tableType := strings.ToLower(option.TableType) |
| 159 | + if tableType == "" { |
| 160 | + tableType = "prefer_ascii" |
| 161 | + } |
| 162 | + if tableType != "prefer_ascii" && tableType != "prefer_entropy" { |
| 163 | + return nil, fmt.Errorf("table-type must be prefer_ascii or prefer_entropy") |
| 164 | + } |
| 165 | + |
| 166 | + seed := option.Seed |
| 167 | + if seed == "" { |
| 168 | + seed = option.Key |
| 169 | + } |
| 170 | + |
| 171 | + table := sudoku.NewTable(seed, tableType) |
| 172 | + |
| 173 | + defaultConf := apis.DefaultConfig() |
| 174 | + paddingMin := defaultConf.PaddingMin |
| 175 | + paddingMax := defaultConf.PaddingMax |
| 176 | + if option.PaddingMin != nil { |
| 177 | + paddingMin = *option.PaddingMin |
| 178 | + } |
| 179 | + if option.PaddingMax != nil { |
| 180 | + paddingMax = *option.PaddingMax |
| 181 | + } |
| 182 | + if option.PaddingMin == nil && option.PaddingMax != nil && paddingMax < paddingMin { |
| 183 | + paddingMin = paddingMax |
| 184 | + } |
| 185 | + if option.PaddingMax == nil && option.PaddingMin != nil && paddingMax < paddingMin { |
| 186 | + paddingMax = paddingMin |
| 187 | + } |
| 188 | + |
| 189 | + baseConf := apis.ProtocolConfig{ |
| 190 | + ServerAddress: net.JoinHostPort(option.Server, strconv.Itoa(option.Port)), |
| 191 | + Key: option.Key, |
| 192 | + AEADMethod: defaultConf.AEADMethod, |
| 193 | + Table: table, |
| 194 | + PaddingMin: paddingMin, |
| 195 | + PaddingMax: paddingMax, |
| 196 | + HandshakeTimeoutSeconds: defaultConf.HandshakeTimeoutSeconds, |
| 197 | + } |
| 198 | + if option.AEADMethod != "" { |
| 199 | + baseConf.AEADMethod = option.AEADMethod |
| 200 | + } |
| 201 | + |
| 202 | + return &Sudoku{ |
| 203 | + Base: &Base{ |
| 204 | + name: option.Name, |
| 205 | + addr: baseConf.ServerAddress, |
| 206 | + tp: C.Sudoku, |
| 207 | + udp: false, |
| 208 | + tfo: option.TFO, |
| 209 | + mpTcp: option.MPTCP, |
| 210 | + iface: option.Interface, |
| 211 | + rmark: option.RoutingMark, |
| 212 | + prefer: C.NewDNSPrefer(option.IPVersion), |
| 213 | + }, |
| 214 | + option: &option, |
| 215 | + table: table, |
| 216 | + baseConf: baseConf, |
| 217 | + }, nil |
| 218 | +} |
| 219 | + |
| 220 | +func buildSudokuHandshakePayload(key string) [16]byte { |
| 221 | + var payload [16]byte |
| 222 | + binary.BigEndian.PutUint64(payload[:8], uint64(time.Now().Unix())) |
| 223 | + hash := sha256.Sum256([]byte(key)) |
| 224 | + copy(payload[8:], hash[:8]) |
| 225 | + return payload |
| 226 | +} |
| 227 | + |
| 228 | +func writeTargetAddress(w io.Writer, rawAddr string) error { |
| 229 | + host, portStr, err := net.SplitHostPort(rawAddr) |
| 230 | + if err != nil { |
| 231 | + return err |
| 232 | + } |
| 233 | + |
| 234 | + portInt, err := net.LookupPort("tcp", portStr) |
| 235 | + if err != nil { |
| 236 | + return err |
| 237 | + } |
| 238 | + |
| 239 | + var buf []byte |
| 240 | + if ip := net.ParseIP(host); ip != nil { |
| 241 | + if ip4 := ip.To4(); ip4 != nil { |
| 242 | + buf = append(buf, 0x01) // IPv4 |
| 243 | + buf = append(buf, ip4...) |
| 244 | + } else { |
| 245 | + buf = append(buf, 0x04) // IPv6 |
| 246 | + buf = append(buf, ip...) |
| 247 | + } |
| 248 | + } else { |
| 249 | + if len(host) > 255 { |
| 250 | + return fmt.Errorf("domain too long") |
| 251 | + } |
| 252 | + buf = append(buf, 0x03) // domain |
| 253 | + buf = append(buf, byte(len(host))) |
| 254 | + buf = append(buf, host...) |
| 255 | + } |
| 256 | + |
| 257 | + var portBytes [2]byte |
| 258 | + binary.BigEndian.PutUint16(portBytes[:], uint16(portInt)) |
| 259 | + buf = append(buf, portBytes[:]...) |
| 260 | + |
| 261 | + _, err = w.Write(buf) |
| 262 | + return err |
| 263 | +} |
0 commit comments