diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0b0788d --- /dev/null +++ b/Makefile @@ -0,0 +1,39 @@ +## +## Copyright (c) 2016 Christian Pointner +## All rights reserved. +## +## Redistribution and use in source and binary forms, with or without +## modification, are permitted provided that the following conditions are met: +## * Redistributions of source code must retain the above copyright +## notice, this list of conditions and the following disclaimer. +## * Redistributions in binary form must reproduce the above copyright +## notice, this list of conditions and the following disclaimer in the +## documentation and/or other materials provided with the distribution. +## * Neither the name of telgo nor the names of its contributors may be +## used to endorse or promote products derived from this software without +## specific prior written permission. +## +## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +## + +GOCMD := go +ifdef GOROOT +GOCMD = $(GOROOT)/bin/go +endif + +bin: + $(GOCMD) build -tags netgo + +clean: + rm -f telme10 + +.PHONY: bin clean diff --git a/main.go b/main.go index 4c9d31a..cf48a03 100644 --- a/main.go +++ b/main.go @@ -31,9 +31,11 @@ package main import ( "fmt" - "github.com/spreadspace/telgo" "os" "time" + + "github.com/coreos/go-systemd/activation" + "github.com/spreadspace/telgo" ) func greet(c *telgo.Client, args []string) bool { @@ -84,10 +86,29 @@ func greet(c *telgo.Client, args []string) bool { func main() { cmdlist := make(telgo.CmdList) - s, err := telgo.NewServer(":7023", "", cmdlist, nil) + listeners, err := activation.Listeners(true) if err != nil { - fmt.Println("failed to initialize the server:", err) - os.Exit(1) + } + + fmt.Printf("got %d sockets from systemd\n", len(listeners)) + var s *telgo.Server + if len(listeners) == 0 { + if len(os.Args) < 2 { + fmt.Println("please specify a address to listen on") + os.Exit(2) + } + if s, err = telgo.NewServer(os.Args[1], "", cmdlist, nil); err != nil { + fmt.Println("failed to initialize the server:", err) + os.Exit(1) + } + } else { + if len(listeners) > 1 { + fmt.Println("warning got more than one socket from systemd, only using the first", err) + } + if s, err = telgo.NewServerFromListener(listeners[0], "", cmdlist, nil); err != nil { + fmt.Println("failed to initialize the server:", err) + os.Exit(1) + } } if err = s.RunWithGreeter(greet); err != nil { fmt.Println("telnet server returned:", err) diff --git a/telme10.service b/telme10.service new file mode 100644 index 0000000..108ef26 --- /dev/null +++ b/telme10.service @@ -0,0 +1,16 @@ +[Unit] +Description=realraum birthday telnet server +After=local-fs.target + +[Service] +User=telme10 +Group=telme10 +Type=simple +ExecStart=/usr/local/bin/telme10 +PrivateTmp=yes +PrivateDevices=yes +ProtectSystem=full +ProtectHome=yes + +[Install] +WantedBy=multi-user.target diff --git a/telme10.socket b/telme10.socket new file mode 100644 index 0000000..63a12a8 --- /dev/null +++ b/telme10.socket @@ -0,0 +1,8 @@ +[Unit] +Description=realraum birthday telnet server socket + +[Socket] +ListenStream=23 + +[Install] +WantedBy=sockets.target