From 63a446d7e5a1b72609679ede2b32e25706d2c7f6 Mon Sep 17 00:00:00 2001 From: Peter Kurfer Date: Sat, 11 Apr 2020 23:29:52 +0200 Subject: [PATCH] Prepare systemd deployment - add systemd service - add default file - improve logging to see what kind of errors might occur - ship multiple prepared config files and replace original one with a symlink - fix current working directory getter --- config.yaml | 2 +- deploy/inetmock.default | 2 + deploy/inetmock.service | 15 ++++++ internal/cmd/init.go | 7 +++ internal/config/loading.go | 4 ++ internal/plugins/loading.go | 1 - mock_config.yaml | 95 +++++++++++++++++++++++++++++++++++++ pkg/path/helpers.go | 3 +- 8 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 deploy/inetmock.default create mode 100644 deploy/inetmock.service create mode 100644 mock_config.yaml diff --git a/config.yaml b/config.yaml index 723047e..2f94e96 100644 --- a/config.yaml +++ b/config.yaml @@ -52,7 +52,7 @@ endpoints: fallback: strategy: incremental args: - startIP: 10.0.0.0 + startIP: 10.0.10.0 dnsOverTlsDowngrade: handler: tls_interceptor listenAddress: 0.0.0.0 diff --git a/deploy/inetmock.default b/deploy/inetmock.default new file mode 100644 index 0000000..54b01bc --- /dev/null +++ b/deploy/inetmock.default @@ -0,0 +1,2 @@ +INETMOCK_PLUGINS_DIRECTORY=/usr/lib/inetmock/plugins +OPTIONS="--config=/etc/inetmock/config.yaml" \ No newline at end of file diff --git a/deploy/inetmock.service b/deploy/inetmock.service new file mode 100644 index 0000000..4a964ec --- /dev/null +++ b/deploy/inetmock.service @@ -0,0 +1,15 @@ +[Unit] +Description=INetMock is a simple service to simulate a valid internet connection + +[Service] +Type=simple +User=inetmock +AmbientCapabilities=CAP_NET_BIND_SERVICE +MemoryMax=50M +CPUQuota=20% +EnvironmentFile=/etc/default/inetmock +ExecStart=/usr/bin/inetmock $OPTIONS + WorkingDirectory=/var/lib/inetmock + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/internal/cmd/init.go b/internal/cmd/init.go index 478bc33..5a5d4e9 100644 --- a/internal/cmd/init.go +++ b/internal/cmd/init.go @@ -6,6 +6,7 @@ import ( "github.com/baez90/inetmock/pkg/path" "github.com/spf13/viper" "go.uber.org/zap" + "os" ) var ( @@ -24,6 +25,11 @@ func initApp() (err error) { ) logger, _ = logging.CreateLogger() registry := plugins.Registry() + + if err = rootCmd.ParseFlags(os.Args); err != nil { + return + } + if err = appConfig.ReadConfig(configFilePath); err != nil { logger.Error( "unrecoverable error occurred during reading the config file", @@ -36,6 +42,7 @@ func initApp() (err error) { pluginDir := viperInst.GetString("plugins-directory") if err = registry.LoadPlugins(pluginDir); err != nil { logger.Error("Failed to load plugins", + zap.String("pluginsDirectory", pluginDir), zap.Error(err), ) } diff --git a/internal/config/loading.go b/internal/config/loading.go index 32fd973..e699b82 100644 --- a/internal/config/loading.go +++ b/internal/config/loading.go @@ -39,6 +39,10 @@ func (c config) InitConfig(flags *pflag.FlagSet) { func (c *config) ReadConfig(configFilePath string) (err error) { if configFilePath != "" && path.FileExists(configFilePath) { + c.logger.Info( + "loading config from passed config file path", + zap.String("configFilePath", configFilePath), + ) viper.SetConfigFile(configFilePath) } if err = viper.ReadInConfig(); err != nil { diff --git a/internal/plugins/loading.go b/internal/plugins/loading.go index 73bb46a..1ef3ad7 100644 --- a/internal/plugins/loading.go +++ b/internal/plugins/loading.go @@ -56,7 +56,6 @@ func (h *handlerRegistry) RegisterHandler(handlerName string, handlerProvider ap } func (h *handlerRegistry) LoadPlugins(pluginsPath string) (err error) { - if !path.DirExists(pluginsPath) { err = fmt.Errorf("plugins path %s does not exist or is not accessible", pluginsPath) return diff --git a/mock_config.yaml b/mock_config.yaml new file mode 100644 index 0000000..deb5295 --- /dev/null +++ b/mock_config.yaml @@ -0,0 +1,95 @@ +endpoints: + plainHttp: + handler: http_mock + listenAddress: 0.0.0.0 + port: 80 + options: + rules: + - pattern: ".*\\.(?i)exe" + response: ./assets/fakeFiles/sample.exe + - pattern: ".*\\.(?i)(jpg|jpeg)" + response: ./assets/fakeFiles/default.jpg + - pattern: ".*\\.(?i)png" + response: ./assets/fakeFiles/default.png + - pattern: ".*\\.(?i)gif" + response: ./assets/fakeFiles/default.gif + - pattern: ".*\\.(?i)ico" + response: ./assets/fakeFiles/default.ico + - pattern: ".*\\.(?i)txt" + response: ./assets/fakeFiles/default.txt + - pattern: ".*" + response: ./assets/fakeFiles/default.html + proxy: + handler: http_proxy + listenAddress: 0.0.0.0 + port: 3128 + options: + rules: + - pattern: ".*\\.(?i)exe" + response: ./assets/fakeFiles/sample.exe + - pattern: ".*\\.(?i)(jpg|jpeg)" + response: ./assets/fakeFiles/default.jpg + - pattern: ".*\\.(?i)png" + response: ./assets/fakeFiles/default.png + - pattern: ".*\\.(?i)gif" + response: ./assets/fakeFiles/default.gif + - pattern: ".*\\.(?i)ico" + response: ./assets/fakeFiles/default.ico + - pattern: ".*\\.(?i)txt" + response: ./assets/fakeFiles/default.txt + - pattern: ".*" + response: ./assets/fakeFiles/default.html + httpsDowngrade: + handler: tls_interceptor + listenAddress: 0.0.0.0 + port: 443 + options: + ecdsaCurve: P256 + validity: + ca: + notBeforeRelative: 17520h + notAfterRelative: 17520h + domain: + notBeforeRelative: 168h + notAfterRelative: 168h + rootCaCert: + publicKey: ./ca.pem + privateKey: ./ca.key + certCachePath: /tmp/inetmock/ + target: + ipAddress: 127.0.0.1 + port: 80 + plainDns: + handler: dns_mock + listenAddress: 0.0.0.0 + port: 53 + options: + rules: + - pattern: ".*\\.google\\.com" + response: 1.1.1.1 + - pattern: ".*\\.reddit\\.com" + response: 2.2.2.2 + fallback: + strategy: incremental + args: + startIP: 10.0.10.0 + dnsOverTlsDowngrade: + handler: tls_interceptor + listenAddress: 0.0.0.0 + port: 853 + options: + ecdsaCurve: P256 + validity: + ca: + notBeforeRelative: 17520h + notAfterRelative: 17520h + domain: + notBeforeRelative: 168h + notAfterRelative: 168h + rootCaCert: + publicKey: ./ca.pem + privateKey: ./ca.key + certCachePath: /tmp/inetmock/ + target: + ipAddress: 127.0.0.1 + port: 53 \ No newline at end of file diff --git a/pkg/path/helpers.go b/pkg/path/helpers.go index 5ca4c62..c19d57c 100644 --- a/pkg/path/helpers.go +++ b/pkg/path/helpers.go @@ -2,11 +2,10 @@ package path import ( "os" - "path/filepath" ) func WorkingDirectory() (cwd string) { - cwd, _ = filepath.Abs(filepath.Dir(os.Args[0])) + cwd, _ = os.Getwd() return }