Skip to content

Commit

Permalink
Support proxy configuration #4 (#27)
Browse files Browse the repository at this point in the history
Sometimes it may be necessary to run under network control or proxy network, so we allow users to customize their own proxy configuration.

---------

Co-authored-by: lihan <[email protected]>
  • Loading branch information
ShizheChang and aooohan authored Jan 22, 2024
1 parent b221f57 commit f992618
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
43 changes: 43 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions internal/config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
proxy:
enable: false
url: http://test
42 changes: 42 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
22 changes: 22 additions & 0 deletions internal/config/proxy.go
Original file line number Diff line number Diff line change
@@ -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"`
}
38 changes: 35 additions & 3 deletions internal/sdk/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -381,13 +408,18 @@ 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,
Record: record,
openSdks: make(map[string]*Sdk),
osType: util.GetOSType(),
archType: util.GetArchType(),
config: c,
}
return manager
}

0 comments on commit f992618

Please sign in to comment.