Skip to content

Commit 8d783c6

Browse files
committed
feat: inbound support grpc(lite)
1 parent 91324b7 commit 8d783c6

File tree

13 files changed

+268
-115
lines changed

13 files changed

+268
-115
lines changed

docs/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,7 @@ listeners:
11611161
uuid: 9d0cb9d0-964f-4ef6-897d-6c6b3ccf9e68
11621162
alterId: 1
11631163
# ws-path: "/" # 如果不为空则开启 websocket 传输层
1164+
# grpc-service-name: "GunService" # 如果不为空则开启 grpc 传输层
11641165
# 下面两项如果填写则开启 tls(需要同时填写)
11651166
# certificate: ./server.crt
11661167
# private-key: ./server.key
@@ -1213,6 +1214,7 @@ listeners:
12131214
uuid: 9d0cb9d0-964f-4ef6-897d-6c6b3ccf9e68
12141215
flow: xtls-rprx-vision
12151216
# ws-path: "/" # 如果不为空则开启 websocket 传输层
1217+
# grpc-service-name: "GunService" # 如果不为空则开启 grpc 传输层
12161218
# 下面两项如果填写则开启 tls(需要同时填写)
12171219
# certificate: ./server.crt
12181220
# private-key: ./server.key
@@ -1248,6 +1250,7 @@ listeners:
12481250
- username: 1
12491251
password: 9d0cb9d0-964f-4ef6-897d-6c6b3ccf9e68
12501252
# ws-path: "/" # 如果不为空则开启 websocket 传输层
1253+
# grpc-service-name: "GunService" # 如果不为空则开启 grpc 传输层
12511254
# 下面两项如果填写则开启 tls(需要同时填写)
12521255
certificate: ./server.crt
12531256
private-key: ./server.key

listener/config/trojan.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ type TrojanUser struct {
1313
}
1414

1515
type TrojanServer struct {
16-
Enable bool
17-
Listen string
18-
Users []TrojanUser
19-
WsPath string
20-
Certificate string
21-
PrivateKey string
22-
RealityConfig reality.Config
23-
MuxOption sing.MuxOption
24-
TrojanSSOption TrojanSSOption
16+
Enable bool
17+
Listen string
18+
Users []TrojanUser
19+
WsPath string
20+
GrpcServiceName string
21+
Certificate string
22+
PrivateKey string
23+
RealityConfig reality.Config
24+
MuxOption sing.MuxOption
25+
TrojanSSOption TrojanSSOption
2526
}
2627

2728
// TrojanSSOption from https://github.com/p4gefau1t/trojan-go/blob/v0.10.6/tunnel/shadowsocks/config.go#L5

listener/config/vless.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ type VlessUser struct {
1414
}
1515

1616
type VlessServer struct {
17-
Enable bool
18-
Listen string
19-
Users []VlessUser
20-
WsPath string
21-
Certificate string
22-
PrivateKey string
23-
RealityConfig reality.Config
24-
MuxOption sing.MuxOption `yaml:"mux-option" json:"mux-option,omitempty"`
17+
Enable bool
18+
Listen string
19+
Users []VlessUser
20+
WsPath string
21+
GrpcServiceName string
22+
Certificate string
23+
PrivateKey string
24+
RealityConfig reality.Config
25+
MuxOption sing.MuxOption `yaml:"mux-option" json:"mux-option,omitempty"`
2526
}
2627

2728
func (t VlessServer) String() string {

listener/config/vmess.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ type VmessUser struct {
1414
}
1515

1616
type VmessServer struct {
17-
Enable bool
18-
Listen string
19-
Users []VmessUser
20-
WsPath string
21-
Certificate string
22-
PrivateKey string
23-
RealityConfig reality.Config
24-
MuxOption sing.MuxOption `yaml:"mux-option" json:"mux-option,omitempty"`
17+
Enable bool
18+
Listen string
19+
Users []VmessUser
20+
WsPath string
21+
GrpcServiceName string
22+
Certificate string
23+
PrivateKey string
24+
RealityConfig reality.Config
25+
MuxOption sing.MuxOption `yaml:"mux-option" json:"mux-option,omitempty"`
2526
}
2627

2728
func (t VmessServer) String() string {

listener/inbound/trojan.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99

1010
type TrojanOption struct {
1111
BaseOption
12-
Users []TrojanUser `inbound:"users"`
13-
WsPath string `inbound:"ws-path,omitempty"`
14-
Certificate string `inbound:"certificate,omitempty"`
15-
PrivateKey string `inbound:"private-key,omitempty"`
16-
RealityConfig RealityConfig `inbound:"reality-config,omitempty"`
17-
MuxOption MuxOption `inbound:"mux-option,omitempty"`
18-
SSOption TrojanSSOption `inbound:"ss-option,omitempty"`
12+
Users []TrojanUser `inbound:"users"`
13+
WsPath string `inbound:"ws-path,omitempty"`
14+
GrpcServiceName string `inbound:"grpc-service-name,omitempty"`
15+
Certificate string `inbound:"certificate,omitempty"`
16+
PrivateKey string `inbound:"private-key,omitempty"`
17+
RealityConfig RealityConfig `inbound:"reality-config,omitempty"`
18+
MuxOption MuxOption `inbound:"mux-option,omitempty"`
19+
SSOption TrojanSSOption `inbound:"ss-option,omitempty"`
1920
}
2021

2122
type TrojanUser struct {
@@ -57,14 +58,15 @@ func NewTrojan(options *TrojanOption) (*Trojan, error) {
5758
Base: base,
5859
config: options,
5960
vs: LC.TrojanServer{
60-
Enable: true,
61-
Listen: base.RawAddress(),
62-
Users: users,
63-
WsPath: options.WsPath,
64-
Certificate: options.Certificate,
65-
PrivateKey: options.PrivateKey,
66-
RealityConfig: options.RealityConfig.Build(),
67-
MuxOption: options.MuxOption.Build(),
61+
Enable: true,
62+
Listen: base.RawAddress(),
63+
Users: users,
64+
WsPath: options.WsPath,
65+
GrpcServiceName: options.GrpcServiceName,
66+
Certificate: options.Certificate,
67+
PrivateKey: options.PrivateKey,
68+
RealityConfig: options.RealityConfig.Build(),
69+
MuxOption: options.MuxOption.Build(),
6870
TrojanSSOption: LC.TrojanSSOption{
6971
Enabled: options.SSOption.Enabled,
7072
Method: options.SSOption.Method,

listener/inbound/vless.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99

1010
type VlessOption struct {
1111
BaseOption
12-
Users []VlessUser `inbound:"users"`
13-
WsPath string `inbound:"ws-path,omitempty"`
14-
Certificate string `inbound:"certificate,omitempty"`
15-
PrivateKey string `inbound:"private-key,omitempty"`
16-
RealityConfig RealityConfig `inbound:"reality-config,omitempty"`
17-
MuxOption MuxOption `inbound:"mux-option,omitempty"`
12+
Users []VlessUser `inbound:"users"`
13+
WsPath string `inbound:"ws-path,omitempty"`
14+
GrpcServiceName string `inbound:"grpc-service-name,omitempty"`
15+
Certificate string `inbound:"certificate,omitempty"`
16+
PrivateKey string `inbound:"private-key,omitempty"`
17+
RealityConfig RealityConfig `inbound:"reality-config,omitempty"`
18+
MuxOption MuxOption `inbound:"mux-option,omitempty"`
1819
}
1920

2021
type VlessUser struct {
@@ -51,14 +52,15 @@ func NewVless(options *VlessOption) (*Vless, error) {
5152
Base: base,
5253
config: options,
5354
vs: LC.VlessServer{
54-
Enable: true,
55-
Listen: base.RawAddress(),
56-
Users: users,
57-
WsPath: options.WsPath,
58-
Certificate: options.Certificate,
59-
PrivateKey: options.PrivateKey,
60-
RealityConfig: options.RealityConfig.Build(),
61-
MuxOption: options.MuxOption.Build(),
55+
Enable: true,
56+
Listen: base.RawAddress(),
57+
Users: users,
58+
WsPath: options.WsPath,
59+
GrpcServiceName: options.GrpcServiceName,
60+
Certificate: options.Certificate,
61+
PrivateKey: options.PrivateKey,
62+
RealityConfig: options.RealityConfig.Build(),
63+
MuxOption: options.MuxOption.Build(),
6264
},
6365
}, nil
6466
}

listener/inbound/vmess.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99

1010
type VmessOption struct {
1111
BaseOption
12-
Users []VmessUser `inbound:"users"`
13-
WsPath string `inbound:"ws-path,omitempty"`
14-
Certificate string `inbound:"certificate,omitempty"`
15-
PrivateKey string `inbound:"private-key,omitempty"`
16-
RealityConfig RealityConfig `inbound:"reality-config,omitempty"`
17-
MuxOption MuxOption `inbound:"mux-option,omitempty"`
12+
Users []VmessUser `inbound:"users"`
13+
WsPath string `inbound:"ws-path,omitempty"`
14+
GrpcServiceName string `inbound:"grpc-service-name,omitempty"`
15+
Certificate string `inbound:"certificate,omitempty"`
16+
PrivateKey string `inbound:"private-key,omitempty"`
17+
RealityConfig RealityConfig `inbound:"reality-config,omitempty"`
18+
MuxOption MuxOption `inbound:"mux-option,omitempty"`
1819
}
1920

2021
type VmessUser struct {
@@ -51,14 +52,15 @@ func NewVmess(options *VmessOption) (*Vmess, error) {
5152
Base: base,
5253
config: options,
5354
vs: LC.VmessServer{
54-
Enable: true,
55-
Listen: base.RawAddress(),
56-
Users: users,
57-
WsPath: options.WsPath,
58-
Certificate: options.Certificate,
59-
PrivateKey: options.PrivateKey,
60-
RealityConfig: options.RealityConfig.Build(),
61-
MuxOption: options.MuxOption.Build(),
55+
Enable: true,
56+
Listen: base.RawAddress(),
57+
Users: users,
58+
WsPath: options.WsPath,
59+
GrpcServiceName: options.GrpcServiceName,
60+
Certificate: options.Certificate,
61+
PrivateKey: options.PrivateKey,
62+
RealityConfig: options.RealityConfig.Build(),
63+
MuxOption: options.MuxOption.Build(),
6264
},
6365
}, nil
6466
}

listener/sing_vless/server.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/metacubex/mihomo/listener/reality"
1919
"github.com/metacubex/mihomo/listener/sing"
2020
"github.com/metacubex/mihomo/log"
21+
"github.com/metacubex/mihomo/transport/gun"
2122
mihomoVMess "github.com/metacubex/mihomo/transport/vmess"
2223

2324
"github.com/metacubex/sing-vmess/vless"
@@ -92,7 +93,7 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition)
9293

9394
tlsConfig := &tls.Config{}
9495
var realityBuilder *reality.Builder
95-
var httpMux *http.ServeMux
96+
var httpHandler http.Handler
9697

9798
if config.Certificate != "" && config.PrivateKey != "" {
9899
cert, err := N.ParseCert(config.Certificate, config.PrivateKey, C.Path)
@@ -111,17 +112,28 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition)
111112
}
112113
}
113114
if config.WsPath != "" {
114-
httpMux = http.NewServeMux()
115+
httpMux := http.NewServeMux()
115116
httpMux.HandleFunc(config.WsPath, func(w http.ResponseWriter, r *http.Request) {
116117
conn, err := mihomoVMess.StreamUpgradedWebsocketConn(w, r)
117118
if err != nil {
118119
http.Error(w, err.Error(), 500)
119120
return
120121
}
121-
sl.HandleConn(conn, tunnel)
122+
sl.HandleConn(conn, tunnel, additions...)
122123
})
124+
httpHandler = httpMux
123125
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "http/1.1")
124126
}
127+
if config.GrpcServiceName != "" {
128+
httpHandler = gun.NewServerHandler(gun.ServerOption{
129+
ServiceName: config.GrpcServiceName,
130+
ConnHandler: func(conn net.Conn) {
131+
sl.HandleConn(conn, tunnel, additions...)
132+
},
133+
HttpHandler: httpHandler,
134+
})
135+
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "h2")
136+
}
125137

126138
for _, addr := range strings.Split(config.Listen, ",") {
127139
addr := addr
@@ -141,8 +153,8 @@ func New(config LC.VlessServer, tunnel C.Tunnel, additions ...inbound.Addition)
141153
sl.listeners = append(sl.listeners, l)
142154

143155
go func() {
144-
if httpMux != nil {
145-
_ = http.Serve(l, httpMux)
156+
if httpHandler != nil {
157+
_ = http.Serve(l, httpHandler)
146158
return
147159
}
148160
for {

listener/sing_vmess/server.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/metacubex/mihomo/listener/reality"
1717
"github.com/metacubex/mihomo/listener/sing"
1818
"github.com/metacubex/mihomo/ntp"
19+
"github.com/metacubex/mihomo/transport/gun"
1920
mihomoVMess "github.com/metacubex/mihomo/transport/vmess"
2021

2122
vmess "github.com/metacubex/sing-vmess"
@@ -76,7 +77,7 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition)
7677

7778
tlsConfig := &tls.Config{}
7879
var realityBuilder *reality.Builder
79-
var httpMux *http.ServeMux
80+
var httpHandler http.Handler
8081

8182
if config.Certificate != "" && config.PrivateKey != "" {
8283
cert, err := N.ParseCert(config.Certificate, config.PrivateKey, C.Path)
@@ -95,17 +96,28 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition)
9596
}
9697
}
9798
if config.WsPath != "" {
98-
httpMux = http.NewServeMux()
99+
httpMux := http.NewServeMux()
99100
httpMux.HandleFunc(config.WsPath, func(w http.ResponseWriter, r *http.Request) {
100101
conn, err := mihomoVMess.StreamUpgradedWebsocketConn(w, r)
101102
if err != nil {
102103
http.Error(w, err.Error(), 500)
103104
return
104105
}
105-
sl.HandleConn(conn, tunnel)
106+
sl.HandleConn(conn, tunnel, additions...)
106107
})
108+
httpHandler = httpMux
107109
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "http/1.1")
108110
}
111+
if config.GrpcServiceName != "" {
112+
httpHandler = gun.NewServerHandler(gun.ServerOption{
113+
ServiceName: config.GrpcServiceName,
114+
ConnHandler: func(conn net.Conn) {
115+
sl.HandleConn(conn, tunnel, additions...)
116+
},
117+
HttpHandler: httpHandler,
118+
})
119+
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "h2")
120+
}
109121

110122
for _, addr := range strings.Split(config.Listen, ",") {
111123
addr := addr
@@ -123,8 +135,8 @@ func New(config LC.VmessServer, tunnel C.Tunnel, additions ...inbound.Addition)
123135
sl.listeners = append(sl.listeners, l)
124136

125137
go func() {
126-
if httpMux != nil {
127-
_ = http.Serve(l, httpMux)
138+
if httpHandler != nil {
139+
_ = http.Serve(l, httpHandler)
128140
return
129141
}
130142
for {

0 commit comments

Comments
 (0)