@@ -18,23 +18,36 @@ import (
1818var IPv4loopback1 = limayaml .IPv4loopback1
1919
2020type Forwarder struct {
21- rules []limatype.PortForward
22- ignoreTCP bool
23- ignoreUDP bool
24- closableListeners * ClosableListeners
21+ rules []limatype.PortForward
22+ ignoreTCP bool
23+ ignoreUDP bool
24+ listeners listenerManager
2525}
2626
2727func NewPortForwarder (rules []limatype.PortForward , ignoreTCP , ignoreUDP bool ) * Forwarder {
28+ return newPortForwarderWithManager (rules , ignoreTCP , ignoreUDP , NewClosableListener ())
29+ }
30+
31+ type listenerManager interface {
32+ Forward (ctx context.Context , dialContext func (ctx context.Context , network string , addr string ) (net.Conn , error ), protocol , hostAddress , guestAddress string )
33+ Remove (ctx context.Context , protocol , hostAddress , guestAddress string )
34+ Close () error
35+ }
36+
37+ func newPortForwarderWithManager (rules []limatype.PortForward , ignoreTCP , ignoreUDP bool , mgr listenerManager ) * Forwarder {
2838 return & Forwarder {
29- rules : rules ,
30- ignoreTCP : ignoreTCP ,
31- ignoreUDP : ignoreUDP ,
32- closableListeners : NewClosableListener () ,
39+ rules : rules ,
40+ ignoreTCP : ignoreTCP ,
41+ ignoreUDP : ignoreUDP ,
42+ listeners : mgr ,
3343 }
3444}
3545
3646func (fw * Forwarder ) Close () error {
37- return fw .closableListeners .Close ()
47+ if fw .listeners == nil {
48+ return nil
49+ }
50+ return fw .listeners .Close ()
3851}
3952
4053func (fw * Forwarder ) OnEvent (ctx context.Context , dialContext func (ctx context.Context , network string , addr string ) (net.Conn , error ), ev * api.Event ) {
@@ -57,7 +70,7 @@ func (fw *Forwarder) OnEvent(ctx context.Context, dialContext func(ctx context.C
5770 continue
5871 }
5972 logrus .Infof ("Forwarding %s from %s to %s" , strings .ToUpper (f .Protocol ), remote , local )
60- fw .closableListeners .Forward (ctx , dialContext , f .Protocol , local , remote )
73+ fw .listeners .Forward (ctx , dialContext , f .Protocol , local , remote )
6174 }
6275 for _ , f := range ev .RemovedLocalPorts {
6376 if fw .shouldIgnoreProtocol (f .Protocol ) {
@@ -67,7 +80,7 @@ func (fw *Forwarder) OnEvent(ctx context.Context, dialContext func(ctx context.C
6780 if local == "" {
6881 continue
6982 }
70- fw .closableListeners .Remove (ctx , f .Protocol , local , remote )
83+ fw .listeners .Remove (ctx , f .Protocol , local , remote )
7184 logrus .Debugf ("Port forwarding closed proto:%s host:%s guest:%s" , f .Protocol , local , remote )
7285 }
7386}
0 commit comments