add GRPCHost config

This commit is contained in:
Zhu Rengen 2021-08-10 14:04:21 +08:00
parent 0ace140df7
commit 39cdfbf6cd
6 changed files with 82 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"context"
"github.com/naiba/nezha/util"
"log"
"time"
@ -46,6 +47,9 @@ func init() {
if dao.Conf.GRPCPort == 0 {
dao.Conf.GRPCPort = 5555
}
if dao.Conf.GRPCHost == "" {
dao.Conf.GRPCHost = util.FetchGeoIP(false).IP
}
dao.Cache = cache.New(5*time.Minute, 10*time.Minute)
initSystem()

View File

@ -39,6 +39,7 @@ type Config struct {
}
HTTPPort uint
GRPCPort uint
GRPCHost string
EnableIPChangeNotification bool
// IP变更提醒

View File

@ -155,7 +155,7 @@ function addOrEditNotification(notification) {
);
}
function addOrEditServer(server) {
function addOrEditServer(server, conf) {
const modal = $(".server.modal");
modal.children(".header").text((server ? "修改" : "添加") + "服务器");
modal
@ -172,9 +172,14 @@ function addOrEditServer(server) {
modal.find("textarea[name=Note]").val(server ? server.Note : null);
if (server) {
modal.find(".secret.field").attr("style", "");
modal.find(".command.field").attr("style", "");
modal.find(".command.hostSecret").text(server.Secret);
modal.find(".command.GRPCHost").text(conf.GRPCHost);
modal.find(".command.GRPCPort").text(conf.GRPCPort);
modal.find("input[name=secret]").val(server.Secret);
} else {
modal.find(".secret.field").attr("style", "display:none");
modal.find(".command.field").attr("style", "display:none");
modal.find("input[name=secret]").val("");
}
showFormModal(".server.modal", "#serverForm", "/api/server");

View File

@ -32,7 +32,7 @@
Shell推荐
</div>
<div class="ui segment">
curl -L https://raw.githubusercontent.com/naiba/nezha/master/script/install.sh -o nezha.sh && chmod +x nezha.sh && sudo ./nezha.sh <code class="command host"></code> 5555 <code class="command hostSecret"></code>
curl -L https://raw.githubusercontent.com/cloverzrg/nezha/master/script/install.sh -o nezha.sh && chmod +x nezha.sh && sudo ./nezha.sh <code class="command GRPCHost"></code> <code class="command GRPCPort"></code> <code class="command hostSecret"></code>
</div>
</div>
</div>

View File

@ -35,7 +35,7 @@
<td style="word-break: break-word;">{{$server.Note}}</td>
<td>
<div class="ui mini icon buttons">
<button class="ui button" onclick="addOrEditServer({{$server.Marshal}})">
<button class="ui button" onclick="addOrEditServer({{$server.Marshal}},{{$.Conf}})">
<i class="edit icon"></i>
</button>
<button class="ui button"

69
util/myip.go Normal file
View File

@ -0,0 +1,69 @@
package util
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/naiba/nezha/pkg/utils"
)
type geoIP struct {
CountryCode string `json:"country_code,omitempty"`
IP string `json:"ip,omitempty"`
Query string `json:"query,omitempty"`
}
var (
geoIPApiList = []string{
"https://api.ip.sb/geoip",
"https://ip.seeip.org/geoip",
"https://ipapi.co/json",
"https://freegeoip.app/json/",
"http://ip-api.com/json/",
"https://extreme-ip-lookup.com/json/",
}
cachedIP, cachedCountry string
httpClientV4 = utils.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false)
httpClientV6 = utils.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, true)
)
func FetchGeoIP(isV6 bool) geoIP {
servers := geoIPApiList
var ip geoIP
var resp *http.Response
var err error
for i := 0; i < len(servers); i++ {
if isV6 {
resp, err = httpClientV6.Get(servers[i])
} else {
resp, err = httpClientV4.Get(servers[i])
}
if err == nil {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
continue
}
resp.Body.Close()
err = json.Unmarshal(body, &ip)
if err != nil {
continue
}
if ip.IP == "" && ip.Query != "" {
ip.IP = ip.Query
}
// 没取到 v6 IP
if isV6 && !strings.Contains(ip.IP, ":") {
continue
}
// 没取到 v4 IP
if !isV6 && !strings.Contains(ip.IP, ".") {
continue
}
return ip
}
}
return ip
}