feat(network): add proxy configuration options and environment variable handling

Signed-off-by: luckfox-eng29 <eng29@luckfox.com>
This commit is contained in:
luckfox-eng29
2026-05-06 21:30:54 +08:00
parent 225ee790d2
commit 95f2b6bada
4 changed files with 94 additions and 15 deletions

View File

@@ -2,6 +2,8 @@ package kvm
import (
"fmt"
"os"
"strings"
"kvm/internal/network"
"kvm/internal/udhcpc"
@@ -17,6 +19,27 @@ var (
networkState *network.NetworkInterfaceState
)
func setProxyEnvVar(key, value string) {
value = strings.TrimSpace(value)
upperKey := strings.ToUpper(key)
if value == "" {
_ = os.Unsetenv(key)
_ = os.Unsetenv(upperKey)
return
}
_ = os.Setenv(key, value)
_ = os.Setenv(upperKey, value)
}
func applyProxyEnvironment(networkConfig *network.NetworkConfig) {
if networkConfig == nil {
return
}
setProxyEnvVar("http_proxy", networkConfig.HTTPProxy.String)
setProxyEnvVar("https_proxy", networkConfig.HTTPSProxy.String)
setProxyEnvVar("all_proxy", networkConfig.ALLProxy.String)
}
func networkStateChanged() {
// do not block the main thread
go waitCtrlAndRequestDisplayUpdate(true)
@@ -33,6 +56,7 @@ func networkStateChanged() {
func initNetwork() error {
ensureConfigLoaded()
applyProxyEnvironment(config.NetworkConfig)
state, err := network.NewNetworkInterfaceState(&network.NetworkInterfaceOptions{
DefaultHostname: GetDefaultHostname(),
@@ -131,6 +155,7 @@ func rpcSetNetworkSettings(settings network.RpcNetworkSettings) (*network.RpcNet
if err := SaveConfig(); err != nil {
return nil, err
}
applyProxyEnvironment(config.NetworkConfig)
return &network.RpcNetworkSettings{NetworkConfig: *config.NetworkConfig}, nil
}