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
This commit is contained in:
Peter 2020-04-11 23:29:52 +02:00
parent ca1ac7d89a
commit 63a446d7e5
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
8 changed files with 125 additions and 4 deletions

View file

@ -52,7 +52,7 @@ endpoints:
fallback: fallback:
strategy: incremental strategy: incremental
args: args:
startIP: 10.0.0.0 startIP: 10.0.10.0
dnsOverTlsDowngrade: dnsOverTlsDowngrade:
handler: tls_interceptor handler: tls_interceptor
listenAddress: 0.0.0.0 listenAddress: 0.0.0.0

2
deploy/inetmock.default Normal file
View file

@ -0,0 +1,2 @@
INETMOCK_PLUGINS_DIRECTORY=/usr/lib/inetmock/plugins
OPTIONS="--config=/etc/inetmock/config.yaml"

15
deploy/inetmock.service Normal file
View file

@ -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

View file

@ -6,6 +6,7 @@ import (
"github.com/baez90/inetmock/pkg/path" "github.com/baez90/inetmock/pkg/path"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.uber.org/zap" "go.uber.org/zap"
"os"
) )
var ( var (
@ -24,6 +25,11 @@ func initApp() (err error) {
) )
logger, _ = logging.CreateLogger() logger, _ = logging.CreateLogger()
registry := plugins.Registry() registry := plugins.Registry()
if err = rootCmd.ParseFlags(os.Args); err != nil {
return
}
if err = appConfig.ReadConfig(configFilePath); err != nil { if err = appConfig.ReadConfig(configFilePath); err != nil {
logger.Error( logger.Error(
"unrecoverable error occurred during reading the config file", "unrecoverable error occurred during reading the config file",
@ -36,6 +42,7 @@ func initApp() (err error) {
pluginDir := viperInst.GetString("plugins-directory") pluginDir := viperInst.GetString("plugins-directory")
if err = registry.LoadPlugins(pluginDir); err != nil { if err = registry.LoadPlugins(pluginDir); err != nil {
logger.Error("Failed to load plugins", logger.Error("Failed to load plugins",
zap.String("pluginsDirectory", pluginDir),
zap.Error(err), zap.Error(err),
) )
} }

View file

@ -39,6 +39,10 @@ func (c config) InitConfig(flags *pflag.FlagSet) {
func (c *config) ReadConfig(configFilePath string) (err error) { func (c *config) ReadConfig(configFilePath string) (err error) {
if configFilePath != "" && path.FileExists(configFilePath) { if configFilePath != "" && path.FileExists(configFilePath) {
c.logger.Info(
"loading config from passed config file path",
zap.String("configFilePath", configFilePath),
)
viper.SetConfigFile(configFilePath) viper.SetConfigFile(configFilePath)
} }
if err = viper.ReadInConfig(); err != nil { if err = viper.ReadInConfig(); err != nil {

View file

@ -56,7 +56,6 @@ func (h *handlerRegistry) RegisterHandler(handlerName string, handlerProvider ap
} }
func (h *handlerRegistry) LoadPlugins(pluginsPath string) (err error) { func (h *handlerRegistry) LoadPlugins(pluginsPath string) (err error) {
if !path.DirExists(pluginsPath) { if !path.DirExists(pluginsPath) {
err = fmt.Errorf("plugins path %s does not exist or is not accessible", pluginsPath) err = fmt.Errorf("plugins path %s does not exist or is not accessible", pluginsPath)
return return

95
mock_config.yaml Normal file
View file

@ -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

View file

@ -2,11 +2,10 @@ package path
import ( import (
"os" "os"
"path/filepath"
) )
func WorkingDirectory() (cwd string) { func WorkingDirectory() (cwd string) {
cwd, _ = filepath.Abs(filepath.Dir(os.Args[0])) cwd, _ = os.Getwd()
return return
} }