mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
tcp/udp fix
This commit is contained in:
parent
6e45f3683c
commit
cbe23d2ed1
3 changed files with 17 additions and 26 deletions
|
@ -83,6 +83,10 @@ func newStreamRouteBase(config *ProxyConfig) (*StreamRouteBase, error) {
|
||||||
dstScheme = config.Scheme
|
dstScheme = config.Scheme
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if srcScheme != dstScheme {
|
||||||
|
return nil, NewNestedError("unsupported").Subjectf("%v -> %v", srcScheme, dstScheme)
|
||||||
|
}
|
||||||
|
|
||||||
return &StreamRouteBase{
|
return &StreamRouteBase{
|
||||||
Alias: config.Alias,
|
Alias: config.Alias,
|
||||||
Type: streamType,
|
Type: streamType,
|
||||||
|
@ -106,14 +110,19 @@ func newStreamRouteBase(config *ProxyConfig) (*StreamRouteBase, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStreamRoute(config *ProxyConfig) (StreamRoute, error) {
|
func NewStreamRoute(config *ProxyConfig) (StreamRoute, error) {
|
||||||
|
base, err := newStreamRouteBase(config)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
switch config.Scheme {
|
switch config.Scheme {
|
||||||
case StreamType_TCP:
|
case StreamType_TCP:
|
||||||
return NewTCPRoute(config)
|
base.StreamImpl = NewTCPRoute(base)
|
||||||
case StreamType_UDP:
|
case StreamType_UDP:
|
||||||
return NewUDPRoute(config)
|
base.StreamImpl = NewUDPRoute(base)
|
||||||
default:
|
default:
|
||||||
return nil, NewNestedError("invalid stream type").Subject(config.Scheme)
|
return nil, NewNestedError("invalid stream type").Subject(config.Scheme)
|
||||||
}
|
}
|
||||||
|
return base, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (route *StreamRouteBase) ListeningUrl() string {
|
func (route *StreamRouteBase) ListeningUrl() string {
|
||||||
|
|
|
@ -16,19 +16,11 @@ type TCPRoute struct {
|
||||||
listener net.Listener
|
listener net.Listener
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTCPRoute(config *ProxyConfig) (StreamRoute, error) {
|
func NewTCPRoute(base *StreamRouteBase) StreamImpl {
|
||||||
base, err := newStreamRouteBase(config)
|
return &TCPRoute{
|
||||||
if err != nil {
|
|
||||||
return nil, NewNestedErrorFrom(err).Subject(config.Alias)
|
|
||||||
}
|
|
||||||
if base.TargetScheme != StreamType_TCP {
|
|
||||||
return nil, NewNestedError("unsupported").Subjectf("tcp -> %s", base.TargetScheme)
|
|
||||||
}
|
|
||||||
base.StreamImpl = &TCPRoute{
|
|
||||||
StreamRouteBase: base,
|
StreamRouteBase: base,
|
||||||
listener: nil,
|
listener: nil,
|
||||||
}
|
}
|
||||||
return base, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (route *TCPRoute) Setup() error {
|
func (route *TCPRoute) Setup() error {
|
||||||
|
@ -44,7 +36,7 @@ func (route *TCPRoute) Accept() (interface{}, error) {
|
||||||
return route.listener.Accept()
|
return route.listener.Accept()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (route *TCPRoute) HandleConnection(c interface{}) error {
|
func (route *TCPRoute) Handle(c interface{}) error {
|
||||||
clientConn := c.(net.Conn)
|
clientConn := c.(net.Conn)
|
||||||
|
|
||||||
defer clientConn.Close()
|
defer clientConn.Close()
|
||||||
|
|
|
@ -26,21 +26,11 @@ type UDPConn struct {
|
||||||
nReceived int
|
nReceived int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUDPRoute(config *ProxyConfig) (StreamRoute, error) {
|
func NewUDPRoute(base *StreamRouteBase) StreamImpl {
|
||||||
base, err := newStreamRouteBase(config)
|
return &UDPRoute{
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if base.TargetScheme != StreamType_UDP {
|
|
||||||
return nil, NewNestedError("unsupported").Subjectf("udp->%s", base.TargetScheme)
|
|
||||||
}
|
|
||||||
|
|
||||||
base.StreamImpl = &UDPRoute{
|
|
||||||
StreamRouteBase: base,
|
StreamRouteBase: base,
|
||||||
connMap: make(map[net.Addr]net.Conn),
|
connMap: make(map[net.Addr]net.Conn),
|
||||||
}
|
}
|
||||||
return base, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (route *UDPRoute) Setup() error {
|
func (route *UDPRoute) Setup() error {
|
||||||
|
@ -83,7 +73,7 @@ func (route *UDPRoute) Accept() (interface{}, error) {
|
||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (route *UDPRoute) HandleConnection(c interface{}) error {
|
func (route *UDPRoute) Handle(c interface{}) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
conn := c.(*UDPConn)
|
conn := c.(*UDPConn)
|
||||||
|
|
Loading…
Add table
Reference in a new issue