mirror of
https://github.com/xtaci/kcptun.git
synced 2024-04-21 12:32:32 +00:00
make parsing generic
This commit is contained in:
+6
-24
@@ -2,11 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
kcp "github.com/xtaci/kcp-go/v5"
|
kcp "github.com/xtaci/kcp-go/v5"
|
||||||
|
"github.com/xtaci/kcptun/generic"
|
||||||
"github.com/xtaci/tcpraw"
|
"github.com/xtaci/tcpraw"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,30 +16,13 @@ func dial(config *Config, block kcp.BlockCrypt) (*kcp.UDPSession, error) {
|
|||||||
dialCount++
|
dialCount++
|
||||||
}()
|
}()
|
||||||
|
|
||||||
remoteAddrMatcher := regexp.MustCompile(`(.*)\:([0-9]{1,5})-?([0-9]{1,5})?`)
|
mp, err := generic.ParseMultiPort(config.RemoteAddr)
|
||||||
matches := remoteAddrMatcher.FindStringSubmatch(config.RemoteAddr)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
var remoteAddr string
|
|
||||||
if len(matches) == 3 { // single port
|
|
||||||
remoteAddr = config.RemoteAddr
|
|
||||||
} else if len(matches) == 4 { // multi port
|
|
||||||
minPort, err := strconv.Atoi(matches[2])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
maxPort, err := strconv.Atoi(matches[3])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if (minPort > maxPort) || minPort > 65535 || maxPort > 65535 || minPort == 0 || maxPort == 0 {
|
|
||||||
return nil, errors.Errorf("invalid port range specified: minport:%v -> maxport %v", minPort, maxPort)
|
|
||||||
}
|
|
||||||
|
|
||||||
// assign remote addr
|
|
||||||
remoteAddr = fmt.Sprintf("%v:%v", matches[1], uint64(minPort)+dialCount%uint64(maxPort-minPort+1))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remoteAddr := fmt.Sprintf("%v:%v", mp.Host, uint64(mp.MinPort)+dialCount%uint64(mp.MaxPort-mp.MinPort+1))
|
||||||
|
|
||||||
if config.TCP {
|
if config.TCP {
|
||||||
conn, err := tcpraw.Dial("tcp", remoteAddr)
|
conn, err := tcpraw.Dial("tcp", remoteAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package generic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MultiPort struct {
|
||||||
|
Host string
|
||||||
|
MinPort uint64
|
||||||
|
MaxPort uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse mulitport listener or dialer
|
||||||
|
func ParseMultiPort(addr string) (*MultiPort, error) {
|
||||||
|
remoteAddrMatcher := regexp.MustCompile(`(.*)\:([0-9]{1,5})-?([0-9]{1,5})?`)
|
||||||
|
matches := remoteAddrMatcher.FindStringSubmatch(addr)
|
||||||
|
|
||||||
|
if len(matches) >= 3 {
|
||||||
|
var minPort, maxPort int
|
||||||
|
minPort, err := strconv.Atoi(matches[2])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
maxPort = minPort
|
||||||
|
|
||||||
|
// multiport assignment
|
||||||
|
if len(matches) >= 4 {
|
||||||
|
maxPort, err = strconv.Atoi(matches[3])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (minPort > maxPort) || minPort > 65535 || maxPort > 65535 || minPort == 0 || maxPort == 0 {
|
||||||
|
return nil, errors.Errorf("invalid port range specified: minport:%v -> maxport %v", minPort, maxPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
mp := new(MultiPort)
|
||||||
|
mp.Host = matches[1]
|
||||||
|
mp.MinPort = uint64(minPort)
|
||||||
|
mp.MaxPort = uint64(maxPort)
|
||||||
|
return mp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.Errorf("malformed address:%v", addr)
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user