mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 20:58:14 -05:00
add GRPCHost config
This commit is contained in:
parent
0ace140df7
commit
39cdfbf6cd
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/naiba/nezha/util"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -46,6 +47,9 @@ func init() {
|
|||||||
if dao.Conf.GRPCPort == 0 {
|
if dao.Conf.GRPCPort == 0 {
|
||||||
dao.Conf.GRPCPort = 5555
|
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)
|
dao.Cache = cache.New(5*time.Minute, 10*time.Minute)
|
||||||
|
|
||||||
initSystem()
|
initSystem()
|
||||||
|
@ -39,6 +39,7 @@ type Config struct {
|
|||||||
}
|
}
|
||||||
HTTPPort uint
|
HTTPPort uint
|
||||||
GRPCPort uint
|
GRPCPort uint
|
||||||
|
GRPCHost string
|
||||||
EnableIPChangeNotification bool
|
EnableIPChangeNotification bool
|
||||||
|
|
||||||
// IP变更提醒
|
// IP变更提醒
|
||||||
|
@ -155,7 +155,7 @@ function addOrEditNotification(notification) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function addOrEditServer(server) {
|
function addOrEditServer(server, conf) {
|
||||||
const modal = $(".server.modal");
|
const modal = $(".server.modal");
|
||||||
modal.children(".header").text((server ? "修改" : "添加") + "服务器");
|
modal.children(".header").text((server ? "修改" : "添加") + "服务器");
|
||||||
modal
|
modal
|
||||||
@ -172,9 +172,14 @@ function addOrEditServer(server) {
|
|||||||
modal.find("textarea[name=Note]").val(server ? server.Note : null);
|
modal.find("textarea[name=Note]").val(server ? server.Note : null);
|
||||||
if (server) {
|
if (server) {
|
||||||
modal.find(".secret.field").attr("style", "");
|
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);
|
modal.find("input[name=secret]").val(server.Secret);
|
||||||
} else {
|
} else {
|
||||||
modal.find(".secret.field").attr("style", "display:none");
|
modal.find(".secret.field").attr("style", "display:none");
|
||||||
|
modal.find(".command.field").attr("style", "display:none");
|
||||||
modal.find("input[name=secret]").val("");
|
modal.find("input[name=secret]").val("");
|
||||||
}
|
}
|
||||||
showFormModal(".server.modal", "#serverForm", "/api/server");
|
showFormModal(".server.modal", "#serverForm", "/api/server");
|
||||||
|
2
resource/template/component/server.html
vendored
2
resource/template/component/server.html
vendored
@ -32,7 +32,7 @@
|
|||||||
Shell(推荐)
|
Shell(推荐)
|
||||||
</div>
|
</div>
|
||||||
<div class="ui segment">
|
<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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -35,7 +35,7 @@
|
|||||||
<td style="word-break: break-word;">{{$server.Note}}</td>
|
<td style="word-break: break-word;">{{$server.Note}}</td>
|
||||||
<td>
|
<td>
|
||||||
<div class="ui mini icon buttons">
|
<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>
|
<i class="edit icon"></i>
|
||||||
</button>
|
</button>
|
||||||
<button class="ui button"
|
<button class="ui button"
|
||||||
|
69
util/myip.go
Normal file
69
util/myip.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user