github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/tproxy/tproxy.go (about)

     1  package tproxy
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
     7  	"github.com/Asutorufa/yuhaiin/pkg/protos/config/listener"
     8  	cl "github.com/Asutorufa/yuhaiin/pkg/protos/config/listener"
     9  )
    10  
    11  type Tproxy struct {
    12  	lis netapi.Listener
    13  
    14  	lisAddr *net.TCPAddr
    15  
    16  	*netapi.ChannelServer
    17  }
    18  
    19  func init() {
    20  	listener.RegisterProtocol(NewTproxy)
    21  }
    22  
    23  func NewTproxy(opt *cl.Inbound_Tproxy) func(netapi.Listener) (netapi.Accepter, error) {
    24  	return func(ii netapi.Listener) (netapi.Accepter, error) {
    25  		t := &Tproxy{
    26  			ChannelServer: netapi.NewChannelServer(),
    27  			lis:           ii,
    28  		}
    29  
    30  		if err := t.newTCP(); err != nil {
    31  			return nil, err
    32  		}
    33  
    34  		if err := t.newUDP(); err != nil {
    35  			t.Close()
    36  			return nil, err
    37  		}
    38  
    39  		return t, nil
    40  	}
    41  }
    42  
    43  func (t *Tproxy) Close() error {
    44  	t.ChannelServer.Close()
    45  	return t.lis.Close()
    46  }