@@ -14,10 +14,11 @@ import (
1414
1515 "github.com/sagernet/sing-box/adapter"
1616 "github.com/sagernet/sing-box/adapter/inbound"
17+ "github.com/sagernet/sing-box/common/listener"
1718 "github.com/sagernet/sing-box/log"
1819 "github.com/sagernet/sing-box/protocol/http"
1920 "github.com/sagernet/sing/common/exceptions"
20- "github.com/sagernet/sing/common/network"
21+ N "github.com/sagernet/sing/common/network"
2122)
2223
2324func RegisterInbound (registry * inbound.Registry ) {
@@ -27,8 +28,10 @@ func RegisterInbound(registry *inbound.Registry) {
2728// Inbound is a wrapper around [http.Inbound] that listens for connections using the Application
2829// Layer Geneva HTTP protocol.
2930type Inbound struct {
30- http.Inbound
31- logger log.ContextLogger
31+ inbound.Adapter
32+ httpInbound * http.Inbound
33+ listener * listener.Listener
34+ logger log.ContextLogger
3235}
3336
3437// NewInbound creates a new algeneva inbound adapter.
@@ -37,23 +40,42 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
3740 if err != nil {
3841 return nil , err
3942 }
40- inbound := httpInbound .(* http.Inbound )
41- return & Inbound {
42- Inbound : * inbound ,
43- logger : logger ,
44- }, nil
43+ a := & Inbound {
44+ Adapter : inbound .NewAdapter (constant .TypeALGeneva , tag ),
45+ httpInbound : httpInbound .(* http.Inbound ),
46+ logger : logger ,
47+ }
48+ a .listener = listener .New (listener.Options {
49+ Context : ctx ,
50+ Logger : logger ,
51+ Network : []string {N .NetworkTCP },
52+ Listen : options .ListenOptions ,
53+ ConnectionHandler : a ,
54+ })
55+ return a , nil
56+ }
57+
58+ func (a * Inbound ) Start (stage adapter.StartStage ) error {
59+ if stage != adapter .StartStateStart {
60+ return nil
61+ }
62+ return a .listener .Start ()
63+ }
64+
65+ func (a * Inbound ) Close () error {
66+ return a .listener .Close ()
4567}
4668
47- func (a * Inbound ) NewConnectionEx (ctx context.Context , conn net.Conn , metadata adapter.InboundContext , onClose network .CloseHandlerFunc ) {
69+ func (a * Inbound ) NewConnectionEx (ctx context.Context , conn net.Conn , metadata adapter.InboundContext , onClose N .CloseHandlerFunc ) {
4870 metadata .Inbound = a .Tag ()
4971 metadata .InboundType = a .Type ()
5072 conn , err := a .newConnectionEx (ctx , conn )
5173 if err != nil {
52- network .CloseOnHandshakeFailure (conn , onClose , err )
74+ N .CloseOnHandshakeFailure (conn , onClose , err )
5375 a .logger .ErrorContext (ctx , exceptions .Cause (err , "process connection from " , metadata .Source ))
5476 return
5577 }
56- a .Inbound .NewConnectionEx (ctx , conn , metadata , onClose )
78+ a .httpInbound .NewConnectionEx (ctx , conn , metadata , onClose )
5779}
5880
5981// newConnectionEx processes the connection and upgrades it to a WebSocket connection.
0 commit comments