Skip to content

Commit

Permalink
Custom config file (#9)
Browse files Browse the repository at this point in the history
* Config file path is now variable

* Custom url instead of hardcoded loopback
  • Loading branch information
mmiranda authored Apr 19, 2022
1 parent 8efa808 commit 78b86d3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
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
Expand Up @@ -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
Expand Down
15 changes: 13 additions & 2 deletions cfdtunnel/cfdtunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions cfdtunnel/cfdtunnel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 78b86d3

Please sign in to comment.