mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 10:08:15 -05:00
style: refine code style
This commit is contained in:
parent
4276ee9464
commit
225d638338
@ -1 +1,7 @@
|
|||||||
package tun
|
package tun
|
||||||
|
|
||||||
|
import "github.com/v2fly/v2ray-core/v5/common/net"
|
||||||
|
|
||||||
|
type Handler interface {
|
||||||
|
Handle(conn net.Conn) error
|
||||||
|
}
|
||||||
|
@ -31,25 +31,40 @@ type TCPHandler struct {
|
|||||||
stack *stack.Stack
|
stack *stack.Stack
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *TCPHandler) SetHandler() {
|
func SetTCPHandler(ctx context.Context, dispatcher routing.Dispatcher, policyManager policy.Manager, config *Config) func(*stack.Stack) error {
|
||||||
tcpForwarder := tcp.NewForwarder(h.stack, rcvWnd, maxInFlight, func(r *tcp.ForwarderRequest) {
|
return func(s *stack.Stack) error {
|
||||||
wg := new(waiter.Queue)
|
tcpForwarder := tcp.NewForwarder(s, rcvWnd, maxInFlight, func(r *tcp.ForwarderRequest) {
|
||||||
linkedEndpoint, err := r.CreateEndpoint(wg)
|
wg := new(waiter.Queue)
|
||||||
if err != nil {
|
linkedEndpoint, err := r.CreateEndpoint(wg)
|
||||||
r.Complete(true)
|
if err != nil {
|
||||||
return
|
r.Complete(true)
|
||||||
}
|
return
|
||||||
defer r.Complete(false)
|
}
|
||||||
|
defer r.Complete(false)
|
||||||
|
|
||||||
// TODO: set sockopt
|
// TODO: set sockopt
|
||||||
|
|
||||||
h.handle(gonet.NewTCPConn(wg, linkedEndpoint))
|
tcpHandler := &TCPHandler{
|
||||||
|
ctx: ctx,
|
||||||
|
dispatcher: dispatcher,
|
||||||
|
policyManager: policyManager,
|
||||||
|
config: config,
|
||||||
|
stack: s,
|
||||||
|
}
|
||||||
|
|
||||||
})
|
if err := tcpHandler.Handle(gonet.NewTCPConn(wg, linkedEndpoint)); err != nil {
|
||||||
h.stack.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpForwarder.HandlePacket)
|
// TODO: log
|
||||||
|
// return newError("failed to handle tcp connection").Base(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
s.SetTransportProtocolHandler(tcp.ProtocolNumber, tcpForwarder.HandlePacket)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *TCPHandler) handle(conn *gonet.TCPConn) error {
|
func (h *TCPHandler) Handle(conn net.Conn) error {
|
||||||
sessionPolicy := h.policyManager.ForLevel(h.config.UserLevel)
|
sessionPolicy := h.policyManager.ForLevel(h.config.UserLevel)
|
||||||
|
|
||||||
addr := conn.RemoteAddr()
|
addr := conn.RemoteAddr()
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
type StackOption func(*stack.Stack) error
|
type StackOption func(*stack.Stack) error
|
||||||
|
|
||||||
func CreateStack(_ stack.LinkEndpoint) (*stack.Stack, error) {
|
func (t *TUN) CreateStack(_ stack.LinkEndpoint) (*stack.Stack, error) {
|
||||||
s := stack.New(stack.Options{
|
s := stack.New(stack.Options{
|
||||||
NetworkProtocols: []stack.NetworkProtocolFactory{
|
NetworkProtocols: []stack.NetworkProtocolFactory{
|
||||||
ipv4.NewProtocol,
|
ipv4.NewProtocol,
|
||||||
@ -25,6 +25,16 @@ func CreateStack(_ stack.LinkEndpoint) (*stack.Stack, error) {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
opts := []StackOption{
|
||||||
|
SetTCPHandler(t.ctx, t.dispatcher, t.policyManager, t.config),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
if err := opt(s); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// nicID := tcpip.NICID(s.UniqueID())
|
// nicID := tcpip.NICID(s.UniqueID())
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
|
@ -40,22 +40,12 @@ func (t *TUN) Start() error {
|
|||||||
return newError("failed to create device").Base(err).AtError()
|
return newError("failed to create device").Base(err).AtError()
|
||||||
}
|
}
|
||||||
|
|
||||||
stack, err := CreateStack(device)
|
stack, err := t.CreateStack(device)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newError("failed to create stack").Base(err).AtError()
|
return newError("failed to create stack").Base(err).AtError()
|
||||||
}
|
}
|
||||||
t.stack = stack
|
t.stack = stack
|
||||||
|
|
||||||
tcpHandler := &TCPHandler{
|
|
||||||
ctx: t.ctx,
|
|
||||||
dispatcher: t.dispatcher,
|
|
||||||
policyManager: t.policyManager,
|
|
||||||
config: t.config,
|
|
||||||
stack: stack,
|
|
||||||
}
|
|
||||||
|
|
||||||
tcpHandler.SetHandler()
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user