simplify setup process with WebUI

This commit is contained in:
yusing
2025-02-14 20:14:16 +08:00
parent 7047d37f70
commit 9f54f40f5a
21 changed files with 590 additions and 451 deletions

56
agent/pkg/env/env.go vendored
View File

@@ -1,11 +1,7 @@
package env
import (
"log"
"net"
"os"
"strings"
"sync"
"github.com/yusing/go-proxy/internal/common"
)
@@ -24,54 +20,6 @@ var (
AgentRegistrationPort = common.GetEnvInt("AGENT_REGISTRATION_PORT", 8891)
AgentSkipClientCertCheck = common.GetEnvBool("AGENT_SKIP_CLIENT_CERT_CHECK", false)
RegistrationAllowedHosts = common.GetCommaSepEnv("REGISTRATION_ALLOWED_HOSTS", "")
RegistrationAllowedCIDRs []*net.IPNet
AgentCACert = common.GetEnvString("AGENT_CA_CERT", "")
AgentSSLCert = common.GetEnvString("AGENT_SSL_CERT", "")
)
func init() {
cidrs, err := toCIDRs(RegistrationAllowedHosts)
if err != nil {
log.Fatalf("failed to parse allowed hosts: %v", err)
}
RegistrationAllowedCIDRs = cidrs
}
func toCIDRs(hosts []string) ([]*net.IPNet, error) {
cidrs := make([]*net.IPNet, 0, len(hosts))
for _, host := range hosts {
if !strings.Contains(host, "/") {
host += "/32"
}
_, cidr, err := net.ParseCIDR(host)
if err != nil {
return nil, err
}
cidrs = append(cidrs, cidr)
}
return cidrs, nil
}
var warnOnce sync.Once
func IsAllowedHost(remoteAddr string) bool {
if len(RegistrationAllowedCIDRs) == 0 {
warnOnce.Do(func() {
log.Println("Warning: REGISTRATION_ALLOWED_HOSTS is empty, allowing all hosts")
})
return true
}
ip, _, err := net.SplitHostPort(remoteAddr)
if err != nil {
ip = remoteAddr
}
netIP := net.ParseIP(ip)
if netIP == nil {
return false
}
for _, cidr := range RegistrationAllowedCIDRs {
if cidr.Contains(netIP) {
return true
}
}
return false
}