|
@@ -5,6 +5,7 @@ import (
|
|
|
"io"
|
|
|
"log"
|
|
|
"net"
|
|
|
+ "strconv"
|
|
|
"sync"
|
|
|
"time"
|
|
|
)
|
|
@@ -14,6 +15,29 @@ func isValidIP(ip string) bool {
|
|
|
return parsedIP != nil
|
|
|
}
|
|
|
|
|
|
+func isValidPort(port string) bool {
|
|
|
+ portInt, err := strconv.Atoi(port)
|
|
|
+ if err != nil {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ if portInt < 1 || portInt > 65535 {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+func isReachable(target string) bool {
|
|
|
+ timeout := time.Duration(2 * time.Second) // Set the timeout value as per your requirement
|
|
|
+ conn, err := net.DialTimeout("tcp", target, timeout)
|
|
|
+ if err != nil {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ defer conn.Close()
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
func connCopy(conn1 net.Conn, conn2 net.Conn, wg *sync.WaitGroup, accumulator *int64) {
|
|
|
io.Copy(conn1, conn2)
|
|
|
conn1.Close()
|
|
@@ -53,11 +77,52 @@ func startListener(address string) (net.Listener, error) {
|
|
|
return server, nil
|
|
|
}
|
|
|
|
|
|
+// Config validator
|
|
|
+func (c *ProxyRelayConfig) ValidateConfigs() error {
|
|
|
+ if c.Mode == ProxyMode_Transport {
|
|
|
+ //Port2Host: PortA int, PortB string
|
|
|
+ if !isValidPort(c.PortA) {
|
|
|
+ return errors.New("first address must be a valid port number")
|
|
|
+ }
|
|
|
+
|
|
|
+ if !isReachable(c.PortB) {
|
|
|
+ return errors.New("second address is unreachable")
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+
|
|
|
+ } else if c.Mode == ProxyMode_Listen {
|
|
|
+ //Port2Port: Both port are port number
|
|
|
+ if !isValidPort(c.PortA) {
|
|
|
+ return errors.New("first address is not a valid port number")
|
|
|
+ }
|
|
|
+
|
|
|
+ if !isValidPort(c.PortB) {
|
|
|
+ return errors.New("second address is not a valid port number")
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ } else if c.Mode == ProxyMode_Starter {
|
|
|
+ //Host2Host: Both have to be hosts
|
|
|
+ if !isReachable(c.PortA) {
|
|
|
+ return errors.New("first address is unreachable")
|
|
|
+ }
|
|
|
+
|
|
|
+ if !isReachable(c.PortB) {
|
|
|
+ return errors.New("second address is unreachable")
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+ } else {
|
|
|
+ return errors.New("invalid mode given")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
/*
|
|
|
portA -> server
|
|
|
portB -> server
|
|
|
*/
|
|
|
func (c *ProxyRelayConfig) Port2port(port1 string, port2 string, stopChan chan bool) error {
|
|
|
+ //Trim the Prefix of : if exists
|
|
|
listen1, err := startListener("0.0.0.0:" + port1)
|
|
|
if err != nil {
|
|
|
return err
|