| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 | package staticimport (	"os"	"strconv"	"strings"	"github.com/langgenius/dify-sandbox/internal/types"	"github.com/langgenius/dify-sandbox/internal/utils/log"	"gopkg.in/yaml.v3")var difySandboxGlobalConfigurations types.DifySandboxGlobalConfigurationsfunc InitConfig(path string) error {	difySandboxGlobalConfigurations = types.DifySandboxGlobalConfigurations{}	// read config file	configFile, err := os.Open(path)	if err != nil {		return err	}	defer configFile.Close()	// parse config file	decoder := yaml.NewDecoder(configFile)	err = decoder.Decode(&difySandboxGlobalConfigurations)	if err != nil {		return err	}	debug, err := strconv.ParseBool(os.Getenv("DEBUG"))	if err == nil {		difySandboxGlobalConfigurations.App.Debug = debug	}	max_workers := os.Getenv("MAX_WORKERS")	if max_workers != "" {		difySandboxGlobalConfigurations.MaxWorkers, _ = strconv.Atoi(max_workers)	}	max_requests := os.Getenv("MAX_REQUESTS")	if max_requests != "" {		difySandboxGlobalConfigurations.MaxRequests, _ = strconv.Atoi(max_requests)	}	port := os.Getenv("SANDBOX_PORT")	if port != "" {		difySandboxGlobalConfigurations.App.Port, _ = strconv.Atoi(port)	}	timeout := os.Getenv("WORKER_TIMEOUT")	if timeout != "" {		difySandboxGlobalConfigurations.WorkerTimeout, _ = strconv.Atoi(timeout)	}	api_key := os.Getenv("API_KEY")	if api_key != "" {		difySandboxGlobalConfigurations.App.Key = api_key	}	python_path := os.Getenv("PYTHON_PATH")	if python_path != "" {		difySandboxGlobalConfigurations.PythonPath = python_path	}	if difySandboxGlobalConfigurations.PythonPath == "" {		difySandboxGlobalConfigurations.PythonPath = "/usr/local/bin/python3"	}	python_lib_path := os.Getenv("PYTHON_LIB_PATH")	if python_lib_path != "" {		difySandboxGlobalConfigurations.PythonLibPaths = strings.Split(python_lib_path, ",")	}	if len(difySandboxGlobalConfigurations.PythonLibPaths) == 0 {		difySandboxGlobalConfigurations.PythonLibPaths = DEFAULT_PYTHON_LIB_REQUIREMENTS	}	python_pip_mirror_url := os.Getenv("PIP_MIRROR_URL")	if python_pip_mirror_url != "" {		difySandboxGlobalConfigurations.PythonPipMirrorURL = python_pip_mirror_url	}	python_deps_update_interval := os.Getenv("PYTHON_DEPS_UPDATE_INTERVAL")	if python_deps_update_interval != "" {		difySandboxGlobalConfigurations.PythonDepsUpdateInterval = python_deps_update_interval	}	// if not set "PythonDepsUpdateInterval", update python dependencies every 30 minutes to keep the sandbox up-to-date	if difySandboxGlobalConfigurations.PythonDepsUpdateInterval == "" {		difySandboxGlobalConfigurations.PythonDepsUpdateInterval = "30m"	}	nodejs_path := os.Getenv("NODEJS_PATH")	if nodejs_path != "" {		difySandboxGlobalConfigurations.NodejsPath = nodejs_path	}	if difySandboxGlobalConfigurations.NodejsPath == "" {		difySandboxGlobalConfigurations.NodejsPath = "/usr/local/bin/node"	}	enable_network := os.Getenv("ENABLE_NETWORK")	if enable_network != "" {		difySandboxGlobalConfigurations.EnableNetwork, _ = strconv.ParseBool(enable_network)	}	enable_preload := os.Getenv("ENABLE_PRELOAD")	if enable_preload != "" {		difySandboxGlobalConfigurations.EnablePreload, _ = strconv.ParseBool(enable_preload)	}	allowed_syscalls := os.Getenv("ALLOWED_SYSCALLS")	if allowed_syscalls != "" {		strs := strings.Split(allowed_syscalls, ",")		ary := make([]int, len(strs))		for i := range ary {			ary[i], err = strconv.Atoi(strs[i])			if err != nil {				return err			}		}		difySandboxGlobalConfigurations.AllowedSyscalls = ary	}	if difySandboxGlobalConfigurations.EnableNetwork {		log.Info("network has been enabled")		socks5_proxy := os.Getenv("SOCKS5_PROXY")		if socks5_proxy != "" {			difySandboxGlobalConfigurations.Proxy.Socks5 = socks5_proxy		}		if difySandboxGlobalConfigurations.Proxy.Socks5 != "" {			log.Info("using socks5 proxy: %s", difySandboxGlobalConfigurations.Proxy.Socks5)		}		https_proxy := os.Getenv("HTTPS_PROXY")		if https_proxy != "" {			difySandboxGlobalConfigurations.Proxy.Https = https_proxy		}		if difySandboxGlobalConfigurations.Proxy.Https != "" {			log.Info("using https proxy: %s", difySandboxGlobalConfigurations.Proxy.Https)		}		http_proxy := os.Getenv("HTTP_PROXY")		if http_proxy != "" {			difySandboxGlobalConfigurations.Proxy.Http = http_proxy		}		if difySandboxGlobalConfigurations.Proxy.Http != "" {			log.Info("using http proxy: %s", difySandboxGlobalConfigurations.Proxy.Http)		}	}	return nil}// avoid global modification, use value copy insteadfunc GetDifySandboxGlobalConfigurations() types.DifySandboxGlobalConfigurations {	return difySandboxGlobalConfigurations}type RunnerDependencies struct {	PythonRequirements string}var runnerDependencies RunnerDependenciesfunc GetRunnerDependencies() RunnerDependencies {	return runnerDependencies}func SetupRunnerDependencies() error {	file, err := os.ReadFile("dependencies/python-requirements.txt")	if err != nil {		if err == os.ErrNotExist {			return nil		}		return err	}	runnerDependencies.PythonRequirements = string(file)	return nil}
 |