网络质量探测 1.1.3:自更新、HTTP/3 与中心节点
版本名 1.1.3,构建号 6。应用标识和签名未改。包含自动更新、覆盖安装、目标历史、IPv4/IPv6 开关、HTTP(含 HTTP/3)以及中心服务端一键添加节点。
This commit is contained in:
1 parent
943259774f
commit
8cdc0f436f
66 files changed
+5489
-78
No files matched your search
+86
-6
@@ -1,12 +1,14 @@
|
||||
// Copyright (C) 2026 灸 言
|
||||
// Copyright (C) 2026 言灸
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -19,6 +21,12 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
if hashRequested(os.Args[1:]) {
|
||||
if err := printHash(os.Stdin); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
cfg, err := loadConfig(os.Args[1:], os.Environ())
|
||||
if err != nil {
|
||||
if errors.Is(err, errHelp) {
|
||||
@@ -26,6 +34,19 @@ func main() {
|
||||
}
|
||||
log.Fatal(err)
|
||||
}
|
||||
if cfg.CenterKey == "" && cfg.CenterDir != "" {
|
||||
key, err := loadCenterKeyFile(cfg.CenterDir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
cfg.CenterKey = key
|
||||
}
|
||||
if cfg.Mode == "helper" {
|
||||
if err := runHelper(os.Stdin, os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
log.Printf("netquality %s", appVersion)
|
||||
log.Printf("%s", cfg.Summary())
|
||||
|
||||
@@ -51,11 +72,27 @@ func main() {
|
||||
}
|
||||
|
||||
var httpSrv *http.Server
|
||||
var node *Node
|
||||
if cfg.Mode == "node" || cfg.Mode == "both" {
|
||||
node := newNode(cfg.NodeToken, cfg.AllowPrivate, extra, defaultLimits())
|
||||
node = newNode(cfg.NodeToken, cfg.AllowPrivate, extra, defaultLimits())
|
||||
node.CenterKey = []byte(cfg.CenterKey)
|
||||
node.ServerID = strings.TrimSpace(cfg.ServerID)
|
||||
if cfg.CenterKey != "" && node.ServerID == "" {
|
||||
log.Printf("设置了中心密钥但没有 NQ_SERVER_ID,将不接受中心签发的短期凭证")
|
||||
}
|
||||
}
|
||||
var center *Center
|
||||
if strings.TrimSpace(cfg.CenterDir) != "" && cfg.Mode != "reflector" {
|
||||
center = newCenter(cfg.CenterDir, cfg.CenterKey)
|
||||
if _, err := center.load(); err != nil {
|
||||
log.Fatalf("中心配置无法读取: %v", err)
|
||||
}
|
||||
log.Printf("center directory %s", cfg.CenterDir)
|
||||
}
|
||||
if node != nil || center != nil {
|
||||
httpSrv = &http.Server{
|
||||
Addr: cfg.ControlAddr,
|
||||
Handler: node.Handler(),
|
||||
Handler: controlHandler(node, center),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
IdleTimeout: 2 * time.Minute,
|
||||
}
|
||||
@@ -76,14 +113,17 @@ func main() {
|
||||
errCh <- serveErr
|
||||
}
|
||||
}()
|
||||
if node.Public() && !loopbackHost(cfg.ControlAddr) {
|
||||
if node != nil && node.Public() && !loopbackHost(cfg.ControlAddr) {
|
||||
log.Printf("WARNING: this node is PUBLIC. Anyone who can reach %s can start bounded probes. Set NQ_NODE_TOKEN or -node-token to require a control token. Caps stay on: jobs=%d duration=%s interval>=%s probes/s=%d bandwidth=%dB/s",
|
||||
cfg.ControlAddr, node.Limits.MaxJobs, node.Limits.MaxDuration, node.Limits.MinInterval, node.Limits.MaxProbePerSec, node.Limits.MaxBytesPerSec)
|
||||
} else if node.Public() {
|
||||
} else if node != nil && node.Public() {
|
||||
log.Printf("node is public and bound to loopback; no control token is required")
|
||||
} else {
|
||||
} else if node != nil {
|
||||
log.Printf("node is private; a control token is required and is not logged")
|
||||
}
|
||||
if center != nil {
|
||||
log.Printf("center API is on the control port; use HTTPS before exposing it")
|
||||
}
|
||||
if cfg.AllowPrivate {
|
||||
log.Printf("private and loopback targets are allowed; metadata addresses stay blocked")
|
||||
}
|
||||
@@ -189,6 +229,46 @@ func parseNets(s string) ([]*net.IPNet, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func controlHandler(node *Node, center *Center) http.Handler {
|
||||
switch {
|
||||
case node != nil && center == nil:
|
||||
return node.Handler()
|
||||
case node == nil && center != nil:
|
||||
return center.Handler()
|
||||
default:
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/v1/center/", center.Handler())
|
||||
mux.Handle("/v1/", node.Handler())
|
||||
return mux
|
||||
}
|
||||
}
|
||||
|
||||
func hashRequested(args []string) bool {
|
||||
for _, arg := range args {
|
||||
if arg == "-hash-secret" || arg == "--hash-secret" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func printHash(r io.Reader) error {
|
||||
sc := bufio.NewScanner(r)
|
||||
sc.Buffer(make([]byte, 64*1024), 1<<20)
|
||||
if !sc.Scan() {
|
||||
if err := sc.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
return errors.New("请从标准输入传入一行要计算哈希的内容")
|
||||
}
|
||||
line := strings.TrimRight(sc.Text(), "\r")
|
||||
if strings.TrimSpace(line) == "" {
|
||||
return errors.New("内容是空的")
|
||||
}
|
||||
fmt.Println(hashSecret(line))
|
||||
return nil
|
||||
}
|
||||
|
||||
func loopbackHost(addr string) bool {
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user