Selaa lähdekoodia

auto update script executed

Toby Chui 1 vuosi sitten
vanhempi
commit
0fa6211df8
5 muutettua tiedostoa jossa 134 lisäystä ja 9 poistoa
  1. 54 0
      mod/tcpprox/conn.go
  2. 64 2
      mod/tcpprox/handler.go
  3. 4 5
      mod/tcpprox/tcpprox.go
  4. 7 0
      web/components/tcpprox.html
  5. 5 2
      web/index.html

+ 54 - 0
mod/tcpprox/conn.go

@@ -77,6 +77,10 @@ func startListener(address string) (net.Listener, error) {
 	return server, nil
 }
 
+/*
+	Config Functions
+*/
+
 // Config validator
 func (c *ProxyRelayConfig) ValidateConfigs() error {
 	if c.Mode == ProxyMode_Transport {
@@ -117,6 +121,56 @@ func (c *ProxyRelayConfig) ValidateConfigs() error {
 	}
 }
 
+// Start a proxy if stopped
+func (c *ProxyRelayConfig) Start() error {
+	if c.Running {
+		return errors.New("proxy already running")
+	}
+
+	// Create a stopChan to control the loop
+	stopChan := make(chan bool)
+	c.stopChan = stopChan
+
+	//Validate configs
+	err := c.ValidateConfigs()
+	if err != nil {
+		return err
+	}
+
+	//Start the proxy service
+	go func() {
+		c.Running = true
+		if c.Mode == ProxyMode_Transport {
+			err = c.Port2host(c.PortA, c.PortB, stopChan)
+		} else if c.Mode == ProxyMode_Listen {
+			err = c.Port2port(c.PortA, c.PortB, stopChan)
+		} else if c.Mode == ProxyMode_Starter {
+			err = c.Host2host(c.PortA, c.PortB, stopChan)
+		}
+		if err != nil {
+			c.Running = false
+			log.Println("Error starting proxy service " + c.Name + "(" + c.UUID + "): " + err.Error())
+		}
+	}()
+
+	//Successfully spawned off the proxy routine
+	return nil
+}
+
+// Stop a running proxy if running
+func (c *ProxyRelayConfig) Stop() {
+	if c.Running || c.stopChan != nil {
+		c.stopChan <- true
+		time.Sleep(300 * time.Millisecond)
+		c.stopChan = nil
+		c.Running = false
+	}
+}
+
+/*
+	Forwarder Functions
+*/
+
 /*
 portA -> server
 portB -> server

+ 64 - 2
mod/tcpprox/handler.go

@@ -1,6 +1,12 @@
 package tcpprox
 
-import "net/http"
+import (
+	"encoding/json"
+	"net/http"
+	"strconv"
+
+	"imuslab.com/zoraxy/mod/utils"
+)
 
 /*
 	Handler.go
@@ -9,6 +15,62 @@ import "net/http"
 	handler.
 */
 
-func HandleListProxyConfigs(w http.ResponseWriter, r *http.Request) {
+func (m *Manager) HandleAddProxyConfig(w http.ResponseWriter, r *http.Request) {
+	name, err := utils.PostPara(r, "name")
+	if err != nil {
+		utils.SendErrorResponse(w, "name cannot be empty")
+		return
+	}
+
+	portA, err := utils.PostPara(r, "portA")
+	if err != nil {
+		utils.SendErrorResponse(w, "first address cannot be empty")
+		return
+	}
+
+	portB, err := utils.PostPara(r, "portB")
+	if err != nil {
+		utils.SendErrorResponse(w, "second address cannot be empty")
+		return
+	}
+
+	timeoutStr, _ := utils.PostPara(r, "timeout")
+	timeout := m.Options.DefaultTimeout
+	if timeoutStr != "" {
+		timeout, err = strconv.Atoi(timeoutStr)
+		if err != nil {
+			utils.SendErrorResponse(w, "invalid timeout value: "+timeoutStr)
+			return
+		}
+	}
+
+	modeValue := ProxyMode_Transport
+	mode, err := utils.PostPara(r, "mode")
+	if err != nil || mode == "" {
+		utils.SendErrorResponse(w, "no mode given")
+	} else if mode == "listen" {
+		modeValue = ProxyMode_Listen
+	} else if mode == "transport" {
+		modeValue = ProxyMode_Transport
+	} else if mode == "starter" {
+		modeValue = ProxyMode_Starter
+	} else {
+		utils.SendErrorResponse(w, "invalid mode given. Only support listen / transport / starter")
+	}
+
+	//Create the target config
+	newConfigUUID := m.NewConfig(&ProxyRelayOptions{
+		Name:    name,
+		PortA:   portA,
+		PortB:   portB,
+		Timeout: timeout,
+		Mode:    modeValue,
+	})
+
+	js, _ := json.Marshal(newConfigUUID)
+	utils.SendJSONResponse(w, string(js))
+}
+
+func (m *Manager) HandleListProxyConfigs(w http.ResponseWriter, r *http.Request) {
 
 }

+ 4 - 5
mod/tcpprox/tcpprox.go

@@ -37,12 +37,13 @@ type ProxyRelayConfig struct {
 	PortB                     string    //Ports B (config depends on mode)
 	Mode                      int       //Operation Mode
 	Timeout                   int       //Timeout for connection in sec
-	StopChan                  chan bool //Stop channel to stop the listener
+	stopChan                  chan bool //Stop channel to stop the listener
 	accumulatedByteTransfered int64     //The total number of bytes transfered
 }
 
 type Options struct {
-	Database *database.Database
+	Database       *database.Database
+	DefaultTimeout int
 }
 
 type Manager struct {
@@ -62,8 +63,6 @@ func NewTCProxy(options *Options) *Manager {
 		options.Database.Read("tcprox", "rules", &previousRules)
 	}
 
-	//TODO: Remove debug
-
 	return &Manager{
 		Options:     options,
 		Configs:     previousRules,
@@ -82,7 +81,7 @@ func (m *Manager) NewConfig(config *ProxyRelayOptions) string {
 		PortB:                     config.PortB,
 		Mode:                      config.Mode,
 		Timeout:                   config.Timeout,
-		StopChan:                  nil,
+		stopChan:                  nil,
 		accumulatedByteTransfered: 0,
 	}
 	m.Configs = append(m.Configs, &thisConfig)

+ 7 - 0
web/components/tcpprox.html

@@ -0,0 +1,7 @@
+<div class="standardContainer">
+    <div class="ui basic segment">
+        <h2>TCP Proxy</h2>
+        <p>Proxy traffic flow on layer 3 via TCP/IP</p>
+    </div>
+
+</div>

+ 5 - 2
web/index.html

@@ -65,8 +65,8 @@
                     <a class="item" tag="">
                         <i class="simplistic remove icon"></i> HTTP over Websocket
                     </a>
-                    <a class="item" tag="">
-                        <i class="simplistic remove icon"></i> TCP Proxy
+                    <a class="item" tag="tcpprox">
+                        <i class="randoms icon"></i> TCP Proxy
                     </a>
                     <div class="ui divider menudivider">Others</div>
                     <a class="item" tag="utm">
@@ -114,6 +114,9 @@
                 <!-- Global Area Networking -->
                 <div id="gan" class="functiontab" target="gan.html"></div>
 
+                <!-- TCP Proxy -->
+                <div id="tcpprox" class="functiontab" target="tcpprox.html"></div>
+
                 <!-- Up Time Monitor -->
                 <div id="utm" class="functiontab" target="uptime.html"></div>