mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-08-04 12:26:46 +02:00
Merge pull request #719 from jemmy1794/main
Add EnableLogging to Stream Proxy for log control
This commit is contained in:
@@ -48,6 +48,7 @@ func (m *Manager) HandleAddProxyConfig(w http.ResponseWriter, r *http.Request) {
|
|||||||
useTCP, _ := utils.PostBool(r, "useTCP")
|
useTCP, _ := utils.PostBool(r, "useTCP")
|
||||||
useUDP, _ := utils.PostBool(r, "useUDP")
|
useUDP, _ := utils.PostBool(r, "useUDP")
|
||||||
useProxyProtocol, _ := utils.PostBool(r, "useProxyProtocol")
|
useProxyProtocol, _ := utils.PostBool(r, "useProxyProtocol")
|
||||||
|
enableLogging, _ := utils.PostBool(r, "enableLogging")
|
||||||
|
|
||||||
//Create the target config
|
//Create the target config
|
||||||
newConfigUUID := m.NewConfig(&ProxyRelayOptions{
|
newConfigUUID := m.NewConfig(&ProxyRelayOptions{
|
||||||
@@ -58,6 +59,7 @@ func (m *Manager) HandleAddProxyConfig(w http.ResponseWriter, r *http.Request) {
|
|||||||
UseTCP: useTCP,
|
UseTCP: useTCP,
|
||||||
UseUDP: useUDP,
|
UseUDP: useUDP,
|
||||||
UseProxyProtocol: useProxyProtocol,
|
UseProxyProtocol: useProxyProtocol,
|
||||||
|
EnableLogging: enableLogging,
|
||||||
})
|
})
|
||||||
|
|
||||||
js, _ := json.Marshal(newConfigUUID)
|
js, _ := json.Marshal(newConfigUUID)
|
||||||
@@ -78,6 +80,7 @@ func (m *Manager) HandleEditProxyConfigs(w http.ResponseWriter, r *http.Request)
|
|||||||
useTCP, _ := utils.PostBool(r, "useTCP")
|
useTCP, _ := utils.PostBool(r, "useTCP")
|
||||||
useUDP, _ := utils.PostBool(r, "useUDP")
|
useUDP, _ := utils.PostBool(r, "useUDP")
|
||||||
useProxyProtocol, _ := utils.PostBool(r, "useProxyProtocol")
|
useProxyProtocol, _ := utils.PostBool(r, "useProxyProtocol")
|
||||||
|
enableLogging, _ := utils.PostBool(r, "enableLogging")
|
||||||
|
|
||||||
newTimeoutStr, _ := utils.PostPara(r, "timeout")
|
newTimeoutStr, _ := utils.PostPara(r, "timeout")
|
||||||
newTimeout := -1
|
newTimeout := -1
|
||||||
@@ -98,6 +101,7 @@ func (m *Manager) HandleEditProxyConfigs(w http.ResponseWriter, r *http.Request)
|
|||||||
UseTCP: useTCP,
|
UseTCP: useTCP,
|
||||||
UseUDP: useUDP,
|
UseUDP: useUDP,
|
||||||
UseProxyProtocol: useProxyProtocol,
|
UseProxyProtocol: useProxyProtocol,
|
||||||
|
EnableLogging: enableLogging,
|
||||||
NewTimeout: newTimeout,
|
NewTimeout: newTimeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -9,9 +9,22 @@ package streamproxy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (c *ProxyRelayInstance) LogMsg(message string, originalError error) {
|
||||||
|
if !c.EnableLogging {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if originalError != nil {
|
||||||
|
log.Println(message, "error:", originalError)
|
||||||
|
} else {
|
||||||
|
log.Println(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Start a proxy if stopped
|
// Start a proxy if stopped
|
||||||
func (c *ProxyRelayInstance) Start() error {
|
func (c *ProxyRelayInstance) Start() error {
|
||||||
if c.IsRunning() {
|
if c.IsRunning() {
|
||||||
|
@@ -30,6 +30,7 @@ type ProxyRelayOptions struct {
|
|||||||
UseTCP bool
|
UseTCP bool
|
||||||
UseUDP bool
|
UseUDP bool
|
||||||
UseProxyProtocol bool
|
UseProxyProtocol bool
|
||||||
|
EnableLogging bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProxyRuleUpdateConfig is used to update the proxy rule config
|
// ProxyRuleUpdateConfig is used to update the proxy rule config
|
||||||
@@ -41,6 +42,7 @@ type ProxyRuleUpdateConfig struct {
|
|||||||
UseTCP bool //Enable TCP proxy, default to false
|
UseTCP bool //Enable TCP proxy, default to false
|
||||||
UseUDP bool //Enable UDP proxy, default to false
|
UseUDP bool //Enable UDP proxy, default to false
|
||||||
UseProxyProtocol bool //Enable Proxy Protocol, default to false
|
UseProxyProtocol bool //Enable Proxy Protocol, default to false
|
||||||
|
EnableLogging bool //Enable Logging TCP/UDP Message, default to true
|
||||||
NewTimeout int //New timeout for the connection, leave -1 for no change
|
NewTimeout int //New timeout for the connection, leave -1 for no change
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,6 +57,7 @@ type ProxyRelayInstance struct {
|
|||||||
UseTCP bool //Enable TCP proxy
|
UseTCP bool //Enable TCP proxy
|
||||||
UseUDP bool //Enable UDP proxy
|
UseUDP bool //Enable UDP proxy
|
||||||
UseProxyProtocol bool //Enable Proxy Protocol
|
UseProxyProtocol bool //Enable Proxy Protocol
|
||||||
|
EnableLogging bool //Enable logging for ProxyInstance
|
||||||
Timeout int //Timeout for connection in sec
|
Timeout int //Timeout for connection in sec
|
||||||
|
|
||||||
/* Internal */
|
/* Internal */
|
||||||
@@ -176,6 +179,7 @@ func (m *Manager) NewConfig(config *ProxyRelayOptions) string {
|
|||||||
UseTCP: config.UseTCP,
|
UseTCP: config.UseTCP,
|
||||||
UseUDP: config.UseUDP,
|
UseUDP: config.UseUDP,
|
||||||
UseProxyProtocol: config.UseProxyProtocol,
|
UseProxyProtocol: config.UseProxyProtocol,
|
||||||
|
EnableLogging: config.EnableLogging,
|
||||||
Timeout: config.Timeout,
|
Timeout: config.Timeout,
|
||||||
tcpStopChan: nil,
|
tcpStopChan: nil,
|
||||||
udpStopChan: nil,
|
udpStopChan: nil,
|
||||||
@@ -221,6 +225,7 @@ func (m *Manager) EditConfig(newConfig *ProxyRuleUpdateConfig) error {
|
|||||||
foundConfig.UseTCP = newConfig.UseTCP
|
foundConfig.UseTCP = newConfig.UseTCP
|
||||||
foundConfig.UseUDP = newConfig.UseUDP
|
foundConfig.UseUDP = newConfig.UseUDP
|
||||||
foundConfig.UseProxyProtocol = newConfig.UseProxyProtocol
|
foundConfig.UseProxyProtocol = newConfig.UseProxyProtocol
|
||||||
|
foundConfig.EnableLogging = newConfig.EnableLogging
|
||||||
|
|
||||||
if newConfig.NewTimeout != -1 {
|
if newConfig.NewTimeout != -1 {
|
||||||
if newConfig.NewTimeout < 0 {
|
if newConfig.NewTimeout < 0 {
|
||||||
|
@@ -31,16 +31,16 @@ func isValidPort(port string) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func connCopy(conn1 net.Conn, conn2 net.Conn, wg *sync.WaitGroup, accumulator *atomic.Int64) {
|
func (c *ProxyRelayInstance) connCopy(conn1 net.Conn, conn2 net.Conn, wg *sync.WaitGroup, accumulator *atomic.Int64) {
|
||||||
n, err := io.Copy(conn1, conn2)
|
n, err := io.Copy(conn1, conn2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
accumulator.Add(n) //Add to accumulator
|
accumulator.Add(n) //Add to accumulator
|
||||||
conn1.Close()
|
conn1.Close()
|
||||||
log.Println("[←]", "close the connect at local:["+conn1.LocalAddr().String()+"] and remote:["+conn1.RemoteAddr().String()+"]")
|
c.LogMsg("[←] close the connect at local:["+conn1.LocalAddr().String()+"] and remote:["+conn1.RemoteAddr().String()+"]", nil)
|
||||||
//conn2.Close()
|
//conn2.Close()
|
||||||
//log.Println("[←]", "close the connect at local:["+conn2.LocalAddr().String()+"] and remote:["+conn2.RemoteAddr().String()+"]")
|
//c.LogMsg("[←] close the connect at local:["+conn2.LocalAddr().String()+"] and remote:["+conn2.RemoteAddr().String()+"]", nil)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,14 +61,16 @@ func writeProxyProtocolHeaderV1(dst net.Conn, src net.Conn) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func forward(conn1 net.Conn, conn2 net.Conn, aTob *atomic.Int64, bToa *atomic.Int64) {
|
func (c *ProxyRelayInstance) forward(conn1 net.Conn, conn2 net.Conn, aTob *atomic.Int64, bToa *atomic.Int64) {
|
||||||
log.Printf("[+] start transmit. [%s],[%s] <-> [%s],[%s] \n", conn1.LocalAddr().String(), conn1.RemoteAddr().String(), conn2.LocalAddr().String(), conn2.RemoteAddr().String())
|
msg := fmt.Sprintf("[+] start transmit. [%s],[%s] <-> [%s],[%s]",
|
||||||
|
conn1.LocalAddr().String(), conn1.RemoteAddr().String(),
|
||||||
|
conn2.LocalAddr().String(), conn2.RemoteAddr().String())
|
||||||
|
c.LogMsg(msg, nil)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
// wait tow goroutines
|
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
go connCopy(conn1, conn2, &wg, aTob)
|
go c.connCopy(conn1, conn2, &wg, aTob)
|
||||||
go connCopy(conn2, conn1, &wg, bToa)
|
go c.connCopy(conn2, conn1, &wg, bToa)
|
||||||
//blocking when the wg is locked
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,18 +80,18 @@ func (c *ProxyRelayInstance) accept(listener net.Listener) (net.Conn, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
//Check if connection in blacklist or whitelist
|
// Check if connection in blacklist or whitelist
|
||||||
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
|
if addr, ok := conn.RemoteAddr().(*net.TCPAddr); ok {
|
||||||
if !c.parent.Options.AccessControlHandler(conn) {
|
if !c.parent.Options.AccessControlHandler(conn) {
|
||||||
time.Sleep(300 * time.Millisecond)
|
time.Sleep(300 * time.Millisecond)
|
||||||
conn.Close()
|
conn.Close()
|
||||||
log.Println("[x]", "Connection from "+addr.IP.String()+" rejected by access control policy")
|
c.LogMsg("[x] Connection from "+addr.IP.String()+" rejected by access control policy", nil)
|
||||||
return nil, errors.New("Connection from " + addr.IP.String() + " rejected by access control policy")
|
return nil, errors.New("Connection from " + addr.IP.String() + " rejected by access control policy")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("[√]", "accept a new client. remote address:["+conn.RemoteAddr().String()+"], local address:["+conn.LocalAddr().String()+"]")
|
c.LogMsg("[√] accept a new client. remote address:["+conn.RemoteAddr().String()+"], local address:["+conn.LocalAddr().String()+"]", nil)
|
||||||
return conn, err
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func startListener(address string) (net.Listener, error) {
|
func startListener(address string) (net.Listener, error) {
|
||||||
@@ -130,7 +132,7 @@ func (c *ProxyRelayInstance) Port2host(allowPort string, targetAddress string, s
|
|||||||
//Start stop handler
|
//Start stop handler
|
||||||
go func() {
|
go func() {
|
||||||
<-stopChan
|
<-stopChan
|
||||||
log.Println("[x]", "Received stop signal. Exiting Port to Host forwarder")
|
c.LogMsg("[x] Received stop signal. Exiting Port to Host forwarder", nil)
|
||||||
server.Close()
|
server.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -147,32 +149,32 @@ func (c *ProxyRelayInstance) Port2host(allowPort string, targetAddress string, s
|
|||||||
}
|
}
|
||||||
|
|
||||||
go func(targetAddress string) {
|
go func(targetAddress string) {
|
||||||
log.Println("[+]", "start connect host:["+targetAddress+"]")
|
c.LogMsg("[+] start connect host:["+targetAddress+"]", nil)
|
||||||
target, err := net.Dial("tcp", targetAddress)
|
target, err := net.Dial("tcp", targetAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// temporarily unavailable, don't use fatal.
|
// temporarily unavailable, don't use fatal.
|
||||||
log.Println("[x]", "connect target address ["+targetAddress+"] faild. retry in ", c.Timeout, "seconds. ")
|
c.LogMsg("[x] connect target address ["+targetAddress+"] failed. retry in "+strconv.Itoa(c.Timeout)+" seconds.", nil)
|
||||||
conn.Close()
|
conn.Close()
|
||||||
log.Println("[←]", "close the connect at local:["+conn.LocalAddr().String()+"] and remote:["+conn.RemoteAddr().String()+"]")
|
c.LogMsg("[←] close the connect at local:["+conn.LocalAddr().String()+"] and remote:["+conn.RemoteAddr().String()+"]", nil)
|
||||||
time.Sleep(time.Duration(c.Timeout) * time.Second)
|
time.Sleep(time.Duration(c.Timeout) * time.Second)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("[→]", "connect target address ["+targetAddress+"] success.")
|
c.LogMsg("[→] connect target address ["+targetAddress+"] success.", nil)
|
||||||
|
|
||||||
if c.UseProxyProtocol {
|
if c.UseProxyProtocol {
|
||||||
log.Println("[+]", "write proxy protocol header to target address ["+targetAddress+"]")
|
c.LogMsg("[+] write proxy protocol header to target address ["+targetAddress+"]", nil)
|
||||||
err = writeProxyProtocolHeaderV1(target, conn)
|
err = writeProxyProtocolHeaderV1(target, conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("[x]", "Write proxy protocol header faild: ", err)
|
c.LogMsg("[x] Write proxy protocol header failed: "+err.Error(), nil)
|
||||||
target.Close()
|
target.Close()
|
||||||
conn.Close()
|
conn.Close()
|
||||||
log.Println("[←]", "close the connect at local:["+conn.LocalAddr().String()+"] and remote:["+conn.RemoteAddr().String()+"]")
|
c.LogMsg("[←] close the connect at local:["+conn.LocalAddr().String()+"] and remote:["+conn.RemoteAddr().String()+"]", nil)
|
||||||
time.Sleep(time.Duration(c.Timeout) * time.Second)
|
time.Sleep(time.Duration(c.Timeout) * time.Second)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
forward(target, conn, &c.aTobAccumulatedByteTransfer, &c.bToaAccumulatedByteTransfer)
|
c.forward(target, conn, &c.aTobAccumulatedByteTransfer, &c.bToaAccumulatedByteTransfer)
|
||||||
}(targetAddress)
|
}(targetAddress)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -138,12 +138,12 @@ func (c *ProxyRelayInstance) ForwardUDP(address1, address2 string, stopChan chan
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
c.udpClientMap.Store(saddr, conn)
|
c.udpClientMap.Store(saddr, conn)
|
||||||
log.Println("[UDP] Created new connection for client " + saddr)
|
c.LogMsg("[UDP] Created new connection for client "+saddr, nil)
|
||||||
// Fire up routine to manage new connection
|
// Fire up routine to manage new connection
|
||||||
go c.RunUDPConnectionRelay(conn, lisener)
|
go c.RunUDPConnectionRelay(conn, lisener)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Println("[UDP] Found connection for client " + saddr)
|
c.LogMsg("[UDP] Found connection for client "+saddr, nil)
|
||||||
conn = rawConn.(*udpClientServerConn)
|
conn = rawConn.(*udpClientServerConn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -16,6 +16,7 @@
|
|||||||
<th>Target Address</th>
|
<th>Target Address</th>
|
||||||
<th>Mode</th>
|
<th>Mode</th>
|
||||||
<th>Timeout (s)</th>
|
<th>Timeout (s)</th>
|
||||||
|
<th>Enable Logging</th>
|
||||||
<th>Actions</th>
|
<th>Actions</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
@@ -81,6 +82,14 @@
|
|||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<div class="ui toggle checkbox">
|
||||||
|
<input type="checkbox" tabindex="0" name="enableLogging" class="hidden">
|
||||||
|
<label>Enable Logging<br>
|
||||||
|
<small>Enable logging of connection status and errors for this rule</small>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<button id="addStreamProxyButton" class="ui basic button" type="submit"><i class="ui green add icon"></i> Create</button>
|
<button id="addStreamProxyButton" class="ui basic button" type="submit"><i class="ui green add icon"></i> Create</button>
|
||||||
<button id="editStreamProxyButton" class="ui basic button" onclick="confirmEditTCPProxyConfig(event, this);" style="display:none;"><i class="ui green check icon"></i> Update</button>
|
<button id="editStreamProxyButton" class="ui basic button" onclick="confirmEditTCPProxyConfig(event, this);" style="display:none;"><i class="ui green check icon"></i> Update</button>
|
||||||
<button class="ui basic red button" onclick="event.preventDefault(); cancelStreamProxyEdit(event);"><i class="ui red remove icon"></i> Cancel</button>
|
<button class="ui basic red button" onclick="event.preventDefault(); cancelStreamProxyEdit(event);"><i class="ui red remove icon"></i> Cancel</button>
|
||||||
@@ -219,6 +228,10 @@
|
|||||||
row.append($('<td>').text(config.ProxyTargetAddr));
|
row.append($('<td>').text(config.ProxyTargetAddr));
|
||||||
row.append($('<td>').text(modeText));
|
row.append($('<td>').text(modeText));
|
||||||
row.append($('<td>').text(config.Timeout));
|
row.append($('<td>').text(config.Timeout));
|
||||||
|
row.append($('<td>').html(config.EnableLogging ?
|
||||||
|
'<i class="green check icon" title="Logging Enabled"></i>' :
|
||||||
|
'<i class="red times icon" title="Logging Disabled"></i>'
|
||||||
|
));
|
||||||
row.append($('<td>').html(`
|
row.append($('<td>').html(`
|
||||||
${startButton}
|
${startButton}
|
||||||
<button onclick="editTCPProxyConfig('${config.UUID}');" class="ui circular basic mini icon button" title="Edit Config"><i class="edit icon"></i></button>
|
<button onclick="editTCPProxyConfig('${config.UUID}');" class="ui circular basic mini icon button" title="Edit Config"><i class="edit icon"></i></button>
|
||||||
@@ -272,6 +285,14 @@
|
|||||||
$(checkboxEle).checkbox("set unchecked");
|
$(checkboxEle).checkbox("set unchecked");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
}else if (key == "EnableLogging"){
|
||||||
|
let checkboxEle = $("#streamProxyForm input[name=enableLogging]").parent();
|
||||||
|
if (value === true){
|
||||||
|
$(checkboxEle).checkbox("set checked");
|
||||||
|
}else{
|
||||||
|
$(checkboxEle).checkbox("set unchecked");
|
||||||
|
}
|
||||||
|
return;
|
||||||
}else if (key == "ListeningAddress"){
|
}else if (key == "ListeningAddress"){
|
||||||
field = $("#streamProxyForm input[name=listenAddr]");
|
field = $("#streamProxyForm input[name=listenAddr]");
|
||||||
}else if (key == "ProxyTargetAddr"){
|
}else if (key == "ProxyTargetAddr"){
|
||||||
@@ -322,6 +343,7 @@
|
|||||||
useTCP: $("#streamProxyForm input[name=useTCP]")[0].checked ,
|
useTCP: $("#streamProxyForm input[name=useTCP]")[0].checked ,
|
||||||
useUDP: $("#streamProxyForm input[name=useUDP]")[0].checked ,
|
useUDP: $("#streamProxyForm input[name=useUDP]")[0].checked ,
|
||||||
useProxyProtocol: $("#streamProxyForm input[name=useProxyProtocol]")[0].checked ,
|
useProxyProtocol: $("#streamProxyForm input[name=useProxyProtocol]")[0].checked ,
|
||||||
|
enableLogging: $("#streamProxyForm input[name=enableLogging]")[0].checked ,
|
||||||
timeout: parseInt($("#streamProxyForm input[name=timeout]").val().trim()),
|
timeout: parseInt($("#streamProxyForm input[name=timeout]").val().trim()),
|
||||||
},
|
},
|
||||||
success: function(response) {
|
success: function(response) {
|
||||||
|
Reference in New Issue
Block a user