Browse Source

自定义域名和path

tuon 1 year ago
parent
commit
2e6aaa2b95
3 changed files with 34 additions and 10 deletions
  1. 8 2
      README.md
  2. 19 7
      jrebelhandler.go
  3. 7 1
      main.go

+ 8 - 2
README.md

@@ -36,6 +36,12 @@ docker run --rm --env GOPROXY=https://goproxy.cn -v "$PWD":/root -w /root/src/pr
 ## 运行
 默认端口: 12345
 ```shell
-# 自定义端口
+## 自定义端口
 ./license-active-server --port=5555
-```
+```
+
+## 当前面有代理时,自定义域名
+--host=https://xxx/xxx
+
+## 自定义contextPath
+--path=/jrebel

+ 19 - 7
jrebelhandler.go

@@ -8,6 +8,7 @@ import (
 	"net/http"
 	"net/url"
 	"strconv"
+	"strings"
 	"time"
 )
 
@@ -15,17 +16,28 @@ func loggingRequest(tag string, r *http.Request) {
 	fmt.Printf("%s --- %s\n", time.Now(), tag)
 }
 
-func indexHandler(w http.ResponseWriter, r *http.Request) {
-	loggingRequest("indexHandler", r)
-	host := "http://" + r.Host
+// 前面有代理,获取不到正确的域名的情况
+func createIndexHandler(reverseHost string) func(http.ResponseWriter, *http.Request) {
+	return func(w http.ResponseWriter, r *http.Request) {
+		loggingRequest("indexHandler", r)
 
-	w.Header().Set("content-type", "text/html; charset=utf-8")
-	w.WriteHeader(200)
-	html := `<h1>Hello,This is a Jrebel & JetBrains License Server!</h1>
+		var host string
+		if len(reverseHost) > 0 {
+			host = reverseHost
+		} else {
+			host = "http://" + r.Host + r.RequestURI
+		}
+
+		host = strings.TrimSuffix(host, "/")
+
+		w.Header().Set("content-type", "text/html; charset=utf-8")
+		w.WriteHeader(200)
+		html := `<h1>Hello,This is a Jrebel & JetBrains License Server!</h1>
 <p>License Server started at %s
 <p>JRebel 7.1 and earlier version Activation address was: <span style='color:red'>%s/{tokenname}</span>, with any email."
 <p>JRebel 2018.1 and later version Activation address was: %s/{guid}(eg:<span style='color:red'> %s/%s </span>), with any email.`
-	_, _ = fmt.Fprintf(w, html, host, host, host, host, newUUIDV4String())
+		_, _ = fmt.Fprintf(w, html, host, host, host, host, newUUIDV4String())
+	}
 }
 
 func jrebelLeasesHandler(w http.ResponseWriter, r *http.Request) {

+ 7 - 1
main.go

@@ -11,6 +11,7 @@ import (
 func main() {
 	serverPort := 12345
 	path := ""
+	reverseHost := ""
 	if len(os.Args) > 1 {
 		for k, v := range os.Args {
 			if k == 0 {
@@ -20,6 +21,11 @@ func main() {
 			hasPortL := strings.HasPrefix(arg, "--port=")
 			hasPortS := strings.HasPrefix(arg, "-p=")
 			hasPath := strings.HasPrefix(arg, "--path=")
+
+			if strings.HasPrefix(arg, "--host=") {
+				reverseHost = strings.TrimPrefix(arg, "--host=")
+			}
+
 			if hasPortL {
 				i, err := strconv.ParseInt(strings.ReplaceAll(arg, "--port=", ""), 10, 32)
 				if err == nil {
@@ -47,7 +53,7 @@ func main() {
 		path = fmt.Sprintf("/%s", path)
 	}
 
-	http.HandleFunc(path, indexHandler)
+	http.HandleFunc(fmt.Sprintf("%s/", path), createIndexHandler(reverseHost))
 	http.HandleFunc(fmt.Sprintf("%s/jrebel/leases", path), jrebelLeasesHandler)
 	http.HandleFunc(fmt.Sprintf("%s/jrebel/leases/1", path), jrebelLeases1Handler)
 	http.HandleFunc(fmt.Sprintf("%s/agent/leases", path), jrebelLeasesHandler)