Browse Source

auto update script executed

Toby Chui 1 year ago
parent
commit
284c230909
8 changed files with 29 additions and 45 deletions
  1. 1 0
      acme.go
  2. 4 0
      api.go
  3. 0 4
      mod/dynamicproxy/dpcore/dpcore.go
  4. 12 15
      mod/dynamicproxy/proxyRequestHandler.go
  5. 6 7
      mod/netstat/netstat.go
  6. 4 4
      mod/uptime/uptime.go
  7. 0 14
      mod/utils/utils.go
  8. 2 1
      start.go

+ 1 - 0
acme.go

@@ -66,6 +66,7 @@ func acmeRegisterSpecialRoutingRule() {
 			}
 
 			resBody, err := ioutil.ReadAll(res.Body)
+			defer res.Body.Close()
 			if err != nil {
 				fmt.Printf("error reading: %s\n", err)
 				return

+ 4 - 0
api.go

@@ -3,6 +3,7 @@ package main
 import (
 	"encoding/json"
 	"net/http"
+	"net/http/pprof"
 
 	"imuslab.com/zoraxy/mod/acme/acmewizard"
 	"imuslab.com/zoraxy/mod/auth"
@@ -166,6 +167,9 @@ func initAPIs() {
 	http.HandleFunc("/api/conf/export", ExportConfigAsZip)
 	http.HandleFunc("/api/conf/import", ImportConfigFromZip)
 
+	//Debug
+	authRouter.HandleFunc("/api/info/pprof", pprof.Index)
+
 	//If you got APIs to add, append them here
 }
 

+ 0 - 4
mod/dynamicproxy/dpcore/dpcore.go

@@ -14,10 +14,6 @@ import (
 
 var onExitFlushLoop func()
 
-const (
-	defaultTimeout = time.Minute * 5
-)
-
 // ReverseProxy is an HTTP Handler that takes an incoming request and
 // sends it to another server, proxying the response back to the
 // client, support http, also support https tunnel using http.hijacker

+ 12 - 15
mod/dynamicproxy/proxyRequestHandler.go

@@ -169,20 +169,17 @@ func (h *ProxyHandler) proxyRequest(w http.ResponseWriter, r *http.Request, targ
 
 func (h *ProxyHandler) logRequest(r *http.Request, succ bool, statusCode int, forwardType string, target string) {
 	if h.Parent.Option.StatisticCollector != nil {
-		go func() {
-			requestInfo := statistic.RequestInfo{
-				IpAddr:                        geodb.GetRequesterIP(r),
-				RequestOriginalCountryISOCode: h.Parent.Option.GeodbStore.GetRequesterCountryISOCode(r),
-				Succ:                          succ,
-				StatusCode:                    statusCode,
-				ForwardType:                   forwardType,
-				Referer:                       r.Referer(),
-				UserAgent:                     r.UserAgent(),
-				RequestURL:                    r.Host + r.RequestURI,
-				Target:                        target,
-			}
-			h.Parent.Option.StatisticCollector.RecordRequest(requestInfo)
-		}()
-
+		requestInfo := statistic.RequestInfo{
+			IpAddr:                        geodb.GetRequesterIP(r),
+			RequestOriginalCountryISOCode: h.Parent.Option.GeodbStore.GetRequesterCountryISOCode(r),
+			Succ:                          succ,
+			StatusCode:                    statusCode,
+			ForwardType:                   forwardType,
+			Referer:                       r.Referer(),
+			UserAgent:                     r.UserAgent(),
+			RequestURL:                    r.Host + r.RequestURI,
+			Target:                        target,
+		}
+		h.Parent.Option.StatisticCollector.RecordRequest(requestInfo)
 	}
 }

+ 6 - 7
mod/netstat/netstat.go

@@ -213,6 +213,7 @@ func GetNetworkInterfaceStats() (int64, int64, error) {
 			out, err := cmd.Output()
 			if err != nil {
 				callbackChan <- wmicResult{0, 0, err}
+				return
 			}
 
 			//Filter out the first line
@@ -251,18 +252,16 @@ func GetNetworkInterfaceStats() (int64, int64, error) {
 
 		go func() {
 			//Spawn a timer to terminate the cmd process if timeout
-			var timer *time.Timer
-			timer = time.AfterFunc(3*time.Second, func() {
-				timer.Stop()
-				if cmd != nil && cmd.Process != nil {
-					cmd.Process.Kill()
-				}
+			time.Sleep(3 * time.Second)
+			if cmd != nil && cmd.Process != nil {
+				cmd.Process.Kill()
 				callbackChan <- wmicResult{0, 0, errors.New("wmic execution timeout")}
-			})
+			}
 		}()
 
 		result := wmicResult{}
 		result = <-callbackChan
+		cmd = nil
 		if result.Err != nil {
 			log.Println("Unable to extract NIC info from wmic: " + result.Err.Error())
 		}

+ 4 - 4
mod/uptime/uptime.go

@@ -93,8 +93,6 @@ func (m *Monitor) ExecuteUptimeCheck() {
 				Latency:    laterncy,
 			}
 
-			//fmt.Println(thisRecord)
-
 		} else {
 			log.Println("Unknown protocol: " + target.Protocol + ". Skipping")
 			continue
@@ -238,9 +236,11 @@ func getWebsiteStatus(url string) (int, error) {
 			}
 			return 0, err
 		}
-
+		defer resp.Body.Close()
+		status_code := resp.StatusCode
+		return status_code, nil
 	}
+	defer resp.Body.Close()
 	status_code := resp.StatusCode
-	resp.Body.Close()
 	return status_code, nil
 }

+ 0 - 14
mod/utils/utils.go

@@ -1,10 +1,7 @@
 package utils
 
 import (
-	"bufio"
-	"encoding/base64"
 	"errors"
-	"io"
 	"log"
 	"net/http"
 	"os"
@@ -131,17 +128,6 @@ func TimeToString(targetTime time.Time) string {
 	return targetTime.Format("2006-01-02 15:04:05")
 }
 
-func LoadImageAsBase64(filepath string) (string, error) {
-	if !FileExists(filepath) {
-		return "", errors.New("File not exists")
-	}
-	f, _ := os.Open(filepath)
-	reader := bufio.NewReader(f)
-	content, _ := io.ReadAll(reader)
-	encoded := base64.StdEncoding.EncodeToString(content)
-	return string(encoded), nil
-}
-
 // Use for redirections
 func ConstructRelativePathFromRequestURL(requestURI string, redirectionLocation string) string {
 	if strings.Count(requestURI, "/") == 1 {

+ 2 - 1
start.go

@@ -128,7 +128,8 @@ func startupSequence() {
 		BuildVersion: version,
 	}, "")
 	if err != nil {
-		panic(err)
+		log.Println("Unable to startup mDNS service.")
+		log.Fatal(err)
 	}
 
 	//Start initial scanning