diff --git a/go.mod b/go.mod index 2b6b21a3..8e7aa086 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/urfave/cli/v2 v2.26.0 github.com/yuin/gopher-lua v1.1.1 golang.org/x/sys v0.15.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( diff --git a/go.sum b/go.sum index 8583657d..e52c0514 100644 --- a/go.sum +++ b/go.sum @@ -37,8 +37,10 @@ github.com/klauspost/cpuid/v2 v2.0.10/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuOb github.com/klauspost/cpuid/v2 v2.0.12/go.mod h1:g2LTdtYhdyuGPqyWyv7qRAmj1WBqxuObKfj5c0PQa7c= github.com/klauspost/cpuid/v2 v2.2.3 h1:sxCkb+qR91z4vsqw4vGGZlDgPz3G7gjaLyK3V8y70BU= github.com/klauspost/cpuid/v2 v2.2.3/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4= github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4= @@ -139,6 +141,7 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 00000000..a12c5626 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,43 @@ +/* + * Copyright 2024 Han Li and contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package config + +import ( + "gopkg.in/yaml.v3" + "os" + "path/filepath" +) + +type Config struct { + Proxy Proxy `yaml:"proxy"` +} + +const filename = "config.yaml" + +func NewConfig(path string) (*Config, error) { + p := filepath.Join(path, filename) + content, err := os.ReadFile(p) + if err != nil { + return nil, err + } + config := &Config{} + err = yaml.Unmarshal(content, config) + if err != nil { + return nil, err + } + return config, nil +} diff --git a/internal/config/config.yaml b/internal/config/config.yaml new file mode 100644 index 00000000..c8eebad3 --- /dev/null +++ b/internal/config/config.yaml @@ -0,0 +1,3 @@ +proxy: + enable: false + url: http://test \ No newline at end of file diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 00000000..2c365e37 --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,42 @@ +/* + * Copyright 2024 Han Li and contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package config_test + +import ( + "github.com/version-fox/vfox/internal/config" + "testing" +) + +func TestNewConfig(t *testing.T) { + _, err := config.NewConfig("") + if err != nil { + t.Fatal(err) + } +} + +func TestConfig_Proxy(t *testing.T) { + c, err := config.NewConfig("") + if err != nil { + t.Fatal(err) + } + if c.Proxy.Url != "http://test" { + t.Fatal("proxy url is invalid") + } + if !c.Proxy.Enable == false { + t.Fatal("proxy enable is invalid") + } +} diff --git a/internal/config/proxy.go b/internal/config/proxy.go new file mode 100644 index 00000000..b2463482 --- /dev/null +++ b/internal/config/proxy.go @@ -0,0 +1,22 @@ +/* + * Copyright 2024 Han Li and contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package config + +type Proxy struct { + Url string `yaml:"url"` + Enable bool `yaml:"enable"` +} diff --git a/internal/sdk/manager.go b/internal/sdk/manager.go index d39a9b47..1f6cf964 100644 --- a/internal/sdk/manager.go +++ b/internal/sdk/manager.go @@ -19,8 +19,10 @@ package sdk import ( "encoding/json" "fmt" + "github.com/version-fox/vfox/internal/config" "io" "net/http" + "net/url" "os" "path/filepath" "strings" @@ -46,6 +48,7 @@ type Manager struct { Record env.Record osType util.OSType archType util.ArchType + config *config.Config } func (m *Manager) EnvKeys() env.Envs { @@ -260,7 +263,19 @@ func (m *Manager) loadLuaFromFileOrUrl(path string) (string, error) { return "", fmt.Errorf("%s not a lua file", path) } if strings.HasPrefix(path, "https://") || strings.HasPrefix(path, "http://") { - resp, err := http.Get(path) + client := http.Client{} + if m.config.Proxy.Enable { + uri, err := url.Parse(m.config.Proxy.Url) + if err == nil { + transPort := &http.Transport{ + Proxy: http.ProxyURL(uri), + } + client = http.Client{ + Transport: transPort, + } + } + } + resp, err := client.Get(path) if err != nil { return "", err } @@ -297,8 +312,20 @@ func (m *Manager) loadLuaFromFileOrUrl(path string) (string, error) { } func (m *Manager) Available() ([]*Category, error) { - // TODO proxy - resp, err := http.Get(pluginIndexUrl) + // FIX proxy + client := http.Client{} + if m.config.Proxy.Enable { + uri, err := url.Parse(m.config.Proxy.Url) + if err == nil { + transPort := &http.Transport{ + Proxy: http.ProxyURL(uri), + } + client = http.Client{ + Transport: transPort, + } + } + } + resp, err := client.Get(pluginIndexUrl) if err != nil { return nil, fmt.Errorf("get plugin index error: %w", err) } @@ -381,6 +408,10 @@ func newSdkManager(record env.Record, meta *PathMeta) *Manager { if err != nil { panic("Init env manager error") } + c, err := config.NewConfig(meta.ConfigPath) + if err != nil { + panic(fmt.Errorf("init config error: %w", err)) + } manager := &Manager{ PathMeta: meta, EnvManager: envManger, @@ -388,6 +419,7 @@ func newSdkManager(record env.Record, meta *PathMeta) *Manager { openSdks: make(map[string]*Sdk), osType: util.GetOSType(), archType: util.GetArchType(), + config: c, } return manager }