|
| 1 | +package inbound_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "net/netip" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/metacubex/mihomo/adapter/outbound" |
| 9 | + "github.com/metacubex/mihomo/common/utils" |
| 10 | + "github.com/metacubex/mihomo/listener/inbound" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func testInboundTrojan(t *testing.T, inboundOptions inbound.TrojanOption, outboundOptions outbound.TrojanOption) { |
| 15 | + userUUID := utils.NewUUIDV4().String() |
| 16 | + inboundOptions.BaseOption = inbound.BaseOption{ |
| 17 | + NameStr: "trojan_inbound", |
| 18 | + Listen: "127.0.0.1", |
| 19 | + Port: "0", |
| 20 | + } |
| 21 | + inboundOptions.Users = []inbound.TrojanUser{ |
| 22 | + {Username: "test", Password: userUUID}, |
| 23 | + } |
| 24 | + in, err := inbound.NewTrojan(&inboundOptions) |
| 25 | + assert.NoError(t, err) |
| 26 | + |
| 27 | + tunnel := NewHttpTestTunnel() |
| 28 | + defer tunnel.Close() |
| 29 | + |
| 30 | + err = in.Listen(tunnel) |
| 31 | + assert.NoError(t, err) |
| 32 | + defer in.Close() |
| 33 | + |
| 34 | + addrPort, err := netip.ParseAddrPort(in.Address()) |
| 35 | + assert.NoError(t, err) |
| 36 | + |
| 37 | + outboundOptions.Name = "trojan_outbound" |
| 38 | + outboundOptions.Server = addrPort.Addr().String() |
| 39 | + outboundOptions.Port = int(addrPort.Port()) |
| 40 | + outboundOptions.Password = userUUID |
| 41 | + |
| 42 | + out, err := outbound.NewTrojan(outboundOptions) |
| 43 | + assert.NoError(t, err) |
| 44 | + defer out.Close() |
| 45 | + |
| 46 | + tunnel.DoTest(t, out) |
| 47 | +} |
| 48 | + |
| 49 | +func TestInboundTrojan_Tls(t *testing.T) { |
| 50 | + inboundOptions := inbound.TrojanOption{ |
| 51 | + Certificate: tlsCertificate, |
| 52 | + PrivateKey: tlsPrivateKey, |
| 53 | + } |
| 54 | + outboundOptions := outbound.TrojanOption{ |
| 55 | + Fingerprint: tlsFingerprint, |
| 56 | + } |
| 57 | + testInboundTrojan(t, inboundOptions, outboundOptions) |
| 58 | +} |
| 59 | + |
| 60 | +func TestInboundTrojan_Wss1(t *testing.T) { |
| 61 | + inboundOptions := inbound.TrojanOption{ |
| 62 | + Certificate: tlsCertificate, |
| 63 | + PrivateKey: tlsPrivateKey, |
| 64 | + WsPath: "/ws", |
| 65 | + } |
| 66 | + outboundOptions := outbound.TrojanOption{ |
| 67 | + Fingerprint: tlsFingerprint, |
| 68 | + Network: "ws", |
| 69 | + WSOpts: outbound.WSOptions{ |
| 70 | + Path: "/ws", |
| 71 | + }, |
| 72 | + } |
| 73 | + testInboundTrojan(t, inboundOptions, outboundOptions) |
| 74 | +} |
| 75 | + |
| 76 | +func TestInboundTrojan_Wss2(t *testing.T) { |
| 77 | + inboundOptions := inbound.TrojanOption{ |
| 78 | + Certificate: tlsCertificate, |
| 79 | + PrivateKey: tlsPrivateKey, |
| 80 | + WsPath: "/ws", |
| 81 | + GrpcServiceName: "GunService", |
| 82 | + } |
| 83 | + outboundOptions := outbound.TrojanOption{ |
| 84 | + Fingerprint: tlsFingerprint, |
| 85 | + Network: "ws", |
| 86 | + WSOpts: outbound.WSOptions{ |
| 87 | + Path: "/ws", |
| 88 | + }, |
| 89 | + } |
| 90 | + testInboundTrojan(t, inboundOptions, outboundOptions) |
| 91 | +} |
| 92 | + |
| 93 | +func TestInboundTrojan_Grpc1(t *testing.T) { |
| 94 | + inboundOptions := inbound.TrojanOption{ |
| 95 | + Certificate: tlsCertificate, |
| 96 | + PrivateKey: tlsPrivateKey, |
| 97 | + GrpcServiceName: "GunService", |
| 98 | + } |
| 99 | + outboundOptions := outbound.TrojanOption{ |
| 100 | + Fingerprint: tlsFingerprint, |
| 101 | + Network: "grpc", |
| 102 | + GrpcOpts: outbound.GrpcOptions{GrpcServiceName: "GunService"}, |
| 103 | + } |
| 104 | + testInboundTrojan(t, inboundOptions, outboundOptions) |
| 105 | +} |
| 106 | + |
| 107 | +func TestInboundTrojan_Grpc2(t *testing.T) { |
| 108 | + inboundOptions := inbound.TrojanOption{ |
| 109 | + Certificate: tlsCertificate, |
| 110 | + PrivateKey: tlsPrivateKey, |
| 111 | + WsPath: "/ws", |
| 112 | + GrpcServiceName: "GunService", |
| 113 | + } |
| 114 | + outboundOptions := outbound.TrojanOption{ |
| 115 | + Fingerprint: tlsFingerprint, |
| 116 | + Network: "grpc", |
| 117 | + GrpcOpts: outbound.GrpcOptions{GrpcServiceName: "GunService"}, |
| 118 | + } |
| 119 | + testInboundTrojan(t, inboundOptions, outboundOptions) |
| 120 | +} |
| 121 | + |
| 122 | +func TestInboundTrojan_Reality(t *testing.T) { |
| 123 | + inboundOptions := inbound.TrojanOption{ |
| 124 | + RealityConfig: inbound.RealityConfig{ |
| 125 | + Dest: net.JoinHostPort(realityDest, "443"), |
| 126 | + PrivateKey: realityPrivateKey, |
| 127 | + ShortID: []string{realityShortid}, |
| 128 | + ServerNames: []string{realityDest}, |
| 129 | + }, |
| 130 | + } |
| 131 | + outboundOptions := outbound.TrojanOption{ |
| 132 | + SNI: realityDest, |
| 133 | + RealityOpts: outbound.RealityOptions{ |
| 134 | + PublicKey: realityPublickey, |
| 135 | + ShortID: realityShortid, |
| 136 | + }, |
| 137 | + ClientFingerprint: "chrome", |
| 138 | + } |
| 139 | + testInboundTrojan(t, inboundOptions, outboundOptions) |
| 140 | +} |
| 141 | + |
| 142 | +func TestInboundTrojan_Reality_Grpc(t *testing.T) { |
| 143 | + inboundOptions := inbound.TrojanOption{ |
| 144 | + RealityConfig: inbound.RealityConfig{ |
| 145 | + Dest: net.JoinHostPort(realityDest, "443"), |
| 146 | + PrivateKey: realityPrivateKey, |
| 147 | + ShortID: []string{realityShortid}, |
| 148 | + ServerNames: []string{realityDest}, |
| 149 | + }, |
| 150 | + GrpcServiceName: "GunService", |
| 151 | + } |
| 152 | + outboundOptions := outbound.TrojanOption{ |
| 153 | + SNI: realityDest, |
| 154 | + RealityOpts: outbound.RealityOptions{ |
| 155 | + PublicKey: realityPublickey, |
| 156 | + ShortID: realityShortid, |
| 157 | + }, |
| 158 | + ClientFingerprint: "chrome", |
| 159 | + Network: "grpc", |
| 160 | + GrpcOpts: outbound.GrpcOptions{GrpcServiceName: "GunService"}, |
| 161 | + } |
| 162 | + testInboundTrojan(t, inboundOptions, outboundOptions) |
| 163 | +} |
| 164 | + |
| 165 | +func TestInboundTrojan_Tls_TrojanSS(t *testing.T) { |
| 166 | + inboundOptions := inbound.TrojanOption{ |
| 167 | + Certificate: tlsCertificate, |
| 168 | + PrivateKey: tlsPrivateKey, |
| 169 | + SSOption: inbound.TrojanSSOption{ |
| 170 | + Enabled: true, |
| 171 | + Method: "", |
| 172 | + Password: "password", |
| 173 | + }, |
| 174 | + } |
| 175 | + outboundOptions := outbound.TrojanOption{ |
| 176 | + Fingerprint: tlsFingerprint, |
| 177 | + SSOpts: outbound.TrojanSSOption{ |
| 178 | + Enabled: true, |
| 179 | + Method: "", |
| 180 | + Password: "password", |
| 181 | + }, |
| 182 | + } |
| 183 | + testInboundTrojan(t, inboundOptions, outboundOptions) |
| 184 | +} |
| 185 | + |
| 186 | +func TestInboundTrojan_Wss_TrojanSS(t *testing.T) { |
| 187 | + inboundOptions := inbound.TrojanOption{ |
| 188 | + Certificate: tlsCertificate, |
| 189 | + PrivateKey: tlsPrivateKey, |
| 190 | + SSOption: inbound.TrojanSSOption{ |
| 191 | + Enabled: true, |
| 192 | + Method: "", |
| 193 | + Password: "password", |
| 194 | + }, |
| 195 | + WsPath: "/ws", |
| 196 | + } |
| 197 | + outboundOptions := outbound.TrojanOption{ |
| 198 | + Fingerprint: tlsFingerprint, |
| 199 | + SSOpts: outbound.TrojanSSOption{ |
| 200 | + Enabled: true, |
| 201 | + Method: "", |
| 202 | + Password: "password", |
| 203 | + }, |
| 204 | + Network: "ws", |
| 205 | + WSOpts: outbound.WSOptions{ |
| 206 | + Path: "/ws", |
| 207 | + }, |
| 208 | + } |
| 209 | + testInboundTrojan(t, inboundOptions, outboundOptions) |
| 210 | +} |
0 commit comments