Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom config file #9

Merged
merged 5 commits into from
Apr 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage:
status:
patch: off
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: gomod
directory: /
schedule:
interval: daily
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.29
version: v1.45.2

# Optional: working directory, useful for monorepos
# working-directory: somedir
15 changes: 13 additions & 2 deletions cfdtunnel/cfdtunnel.go
Original file line number Diff line number Diff line change
@@ -11,19 +11,22 @@ import (
)

const (
iniConfigFile = ".cfdtunnel/config"
localClientDefaultUrl = "127.0.0.1"
localClientDefaultPort = "5555"
)

var (
// LogLevel sets the level of each log
LogLevel = log.WarnLevel
// IniConfigFile sets the path of config file
IniConfigFile = ".cfdtunnel/config"
)

// TunnelConfig struct stores data to launch cloudflared process such as hostname and port.
// It also stores preset Environment Variables needed to use together with the tunnel consumer.
type TunnelConfig struct {
host string
url string
port string
envVars []string
}
@@ -52,7 +55,7 @@ func init() {
func (args Arguments) Execute() {
log.SetLevel(LogLevel)

config, err := readIniConfigFile(getHomePathIniFile(iniConfigFile))
config, err := readIniConfigFile(getHomePathIniFile(IniConfigFile))

if err != nil {
log.Fatalf("An error occurred reading your INI file: %v", err.Error())
@@ -182,13 +185,21 @@ func (cfg config) readConfigSection(section string) (TunnelConfig, error) {
return port
})

url := secs.Key("url").Validate(func(url string) string {
if len(url) == 0 {
return localClientDefaultUrl
}
return url
})

envVars := []string{}
if secs.Key("env").ValueWithShadows()[0] != "" {
envVars = secs.Key("env").ValueWithShadows()
}

return TunnelConfig{
host: host.String(),
url: url,
port: port,
envVars: envVars,
}, nil
8 changes: 4 additions & 4 deletions cfdtunnel/cfdtunnel_test.go
Original file line number Diff line number Diff line change
@@ -131,7 +131,7 @@ func contains(s []string, e string) bool {
}

func TestProxyTunnel(t *testing.T) {
tunnelConfig := TunnelConfig{"foo.bar", "1234", nil}
tunnelConfig := TunnelConfig{"foo.bar", "127.0.0.1", "1234", nil}
cmd := tunnelConfig.startProxyTunnel()
osPid, _ := os.FindProcess(cmd.Process.Pid)
assert.Equal(t, cmd.Process.Pid, osPid.Pid)
@@ -141,10 +141,10 @@ func TestProxyTunnel(t *testing.T) {

func TestTunnelSamePort(t *testing.T) {

tunnelCfg := TunnelConfig{"foo.bar.first", "1234", nil}
tunnelCfg := TunnelConfig{"foo.bar.first", "127.0.0.1", "1234", nil}
cmd1 := tunnelCfg.startProxyTunnel()

tunnelCfg = TunnelConfig{"foo.bar.first", "1234", nil}
tunnelCfg = TunnelConfig{"foo.bar.first", "127.0.0.1", "1234", nil}
cmd2 := tunnelCfg.startProxyTunnel()

err := cmd2.Wait()
@@ -227,7 +227,7 @@ func TestNewTunnel(t *testing.T) {
// TestSock5ProxyRunning launches the proxy tunnel and try to use it calling google.com
// If the result does not have "connection refused" we assume the proxy is running and responding
func TestSocks5ProxyRunning(t *testing.T) {
tunnelConfig := TunnelConfig{"foo.bar", "1234", nil}
tunnelConfig := TunnelConfig{"foo.bar", "127.0.0.1", "1234", nil}
cmd := tunnelConfig.startProxyTunnel()
dialSocksProxy, err := proxy.SOCKS5("tcp", "127.0.0.1:1234", nil, proxy.Direct)
if err != nil {