-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9852053
Showing
11 changed files
with
917 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: goreleaser | ||
|
||
on: | ||
push: | ||
# run only against tags | ||
tags: | ||
- '*' | ||
|
||
permissions: | ||
contents: write | ||
|
||
jobs: | ||
goreleaser: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 | ||
- run: git fetch --force --tags | ||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version: '>=1.19.2' | ||
cache: true | ||
- uses: goreleaser/goreleaser-action@v2 | ||
with: | ||
distribution: goreleaser | ||
version: latest | ||
args: release --rm-dist | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
dist/ | ||
.telmon-config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
before: | ||
hooks: | ||
# You may remove this if you don't use go modules. | ||
- go mod tidy | ||
|
||
builds: | ||
- env: | ||
- CGO_ENABLED=0 | ||
goos: | ||
- linux | ||
- windows | ||
- darwin | ||
goarch: | ||
- amd64 | ||
- arm64 | ||
flags: | ||
- -mod=vendor | ||
|
||
archives: | ||
- replacements: | ||
darwin: Darwin | ||
linux: Linux | ||
windows: Windows | ||
386: i386 | ||
amd64: x86_64 | ||
|
||
checksum: | ||
name_template: 'checksums.txt' | ||
|
||
release: | ||
prerelease: auto | ||
|
||
universal_binaries: | ||
- replace: true | ||
|
||
snapshot: | ||
name_template: "{{ incpatch .Version }}-next" | ||
|
||
changelog: | ||
sort: asc | ||
filters: | ||
exclude: | ||
- '^docs:' | ||
- '^test:' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module github.com/jackkweyunga/telmon | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/mitchellh/mapstructure v1.5.0 | ||
github.com/reiver/go-telnet v0.0.0-20180421082511-9ff0b2ab096e | ||
gopkg.in/yaml.v3 v3.0.1 | ||
) | ||
|
||
require ( | ||
github.com/fsnotify/fsnotify v1.5.4 // indirect | ||
github.com/hashicorp/hcl v1.0.0 // indirect | ||
github.com/kr/pretty v0.3.0 // indirect | ||
github.com/magiconair/properties v1.8.6 // indirect | ||
github.com/pelletier/go-toml v1.9.5 // indirect | ||
github.com/pelletier/go-toml/v2 v2.0.5 // indirect | ||
github.com/reiver/go-oi v1.0.0 // indirect | ||
github.com/spf13/afero v1.8.2 // indirect | ||
github.com/spf13/cast v1.5.0 // indirect | ||
github.com/spf13/jwalterweatherman v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/spf13/viper v1.13.0 // indirect | ||
github.com/subosito/gotenv v1.4.1 // indirect | ||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect | ||
golang.org/x/text v0.3.7 // indirect | ||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect | ||
gopkg.in/ini.v1 v1.67.0 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/viper" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"time" | ||
) | ||
|
||
type Config struct { | ||
Port int `mapstructure:"port"` | ||
Addr string `mapstructure:"addr"` | ||
Password string `mapstructure:"password"` | ||
Email string `mapstructure:"email"` | ||
Receivers []string `mapstructure:"receivers"` | ||
} | ||
|
||
func main() { | ||
|
||
config, err := LoadConfig() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
fmt.Println(config) | ||
|
||
var ( | ||
port = config.Port | ||
addr = config.Addr | ||
fromEmail = config.Email | ||
password = config.Password | ||
receivers = config.Receivers | ||
) | ||
|
||
c := make(chan os.Signal, 1) | ||
|
||
// Notify on any interrupt | ||
signal.Notify(c, os.Interrupt) | ||
|
||
isShuttingDown := false | ||
started := false | ||
|
||
OUTER: | ||
for { | ||
if isShuttingDown { | ||
break | ||
} | ||
|
||
select { | ||
case s, ok := <-c: | ||
if ok { | ||
fmt.Println("Service is going down.") | ||
fmt.Printf("Received signal %v \n", s) | ||
isShuttingDown = true | ||
c = nil | ||
continue OUTER | ||
} | ||
default: | ||
if !started { | ||
println("Monitoring Service started successfully ...") | ||
started = true | ||
} | ||
|
||
t := time.Now() | ||
fmt.Printf("[%s] running \n", t.Format(time.RFC3339)) | ||
Monitor(addr, port, fromEmail, password, receivers) | ||
time.Sleep(10000 * time.Millisecond) | ||
|
||
} | ||
} | ||
} | ||
|
||
var vp *viper.Viper | ||
|
||
func LoadConfig() (Config, error) { | ||
vp = viper.New() | ||
var config Config | ||
|
||
vp.SetConfigName(".telmon-config") | ||
vp.SetConfigType("yaml") | ||
vp.AddConfigPath(".") | ||
err := vp.ReadInConfig() | ||
if err != nil { | ||
return Config{}, err | ||
} | ||
|
||
err = vp.Unmarshal(&config) | ||
if err != nil { | ||
return Config{}, err | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<samp> | ||
|
||
# telmon | ||
|
||
## What is it? | ||
|
||
Telmon is a simple golang daemon that will monitor a given telnet connection from a telnet client to a telnet server. | ||
It reports any loss in connection via `email` to a list of receivers. | ||
|
||
## How to use it. | ||
|
||
### 1. Installation. | ||
Install telmon binary from GitHub. | ||
|
||
```shell | ||
wget <github-path>.tar.gz | ||
``` | ||
|
||
Extract to `/usr/local/bin/` | ||
|
||
```shell | ||
tar -O /usr/local/bin -xzf <github-path>.tar.gz | ||
``` | ||
|
||
- visit the packages page for system specific binaries. | ||
|
||
In order to use telmon, create a configuration file ``.telmon-config.yaml`` in any directory in which you will run the | ||
telmon binary. | ||
|
||
### 2. Configurations | ||
|
||
```shell | ||
$ mkdir telmon | ||
$ cd telmon | ||
$ nano .telmon-config.yaml | ||
``` | ||
|
||
The content of the configuration file should be as follows. | ||
```yaml | ||
--- | ||
addr: example.com | ||
port: <telnet-port> | ||
email: <sender-email> | ||
password: <sender-email-password> | ||
receivers: | ||
- user-1@example.com | ||
- user-2@example.com | ||
``` | ||
### 3. Run | ||
```shell | ||
$ telmon | ||
``` | ||
|
||
### 4. Supervisord | ||
|
||
Supervisord helps to monitor and control running processes | ||
in any linux machine. For linux users, add a supervisord configuration | ||
file to monitor telmon. | ||
|
||
> create a file /etc/supervisord/conf.d/telmon.conf | ||
Add the content below. | ||
|
||
```editorconfig | ||
[program:telmon] | ||
directory=/path/to/your/telmon/directory | ||
command=telmon | ||
autostart=true | ||
autorestart=true | ||
stderr_logfile=/var/log/telmon.err.log | ||
stdout_logfile=/var/log/telmon.out.log | ||
``` | ||
|
||
Reload supervisord configurations. | ||
```shell | ||
supervisorctl reread | ||
supervisorctl update | ||
``` | ||
|
||
OR .. reload supervisor | ||
```shell | ||
supervisorctl reload | ||
``` | ||
|
||
## Credits | ||
|
||
- The [viper project](https://github.com/spf13/viper) | ||
- The [go-telnet project](https://github.com/reiver/go-telnet) | ||
|
||
</samp> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/smtp" | ||
"os" | ||
) | ||
|
||
func sendMail(msg string, from string, password string, toList []string) { | ||
|
||
host := "smtp.gmail.com" | ||
port := "587" | ||
|
||
// We can't send strings directly in mail, | ||
// strings need to be converted into slice bytes | ||
body := []byte(msg) | ||
|
||
// PlainAuth uses the given username and password to | ||
// authenticate to host and act as identity. | ||
// Usually identity should be the empty string, | ||
// to act as username. | ||
auth := smtp.PlainAuth("", from, password, host) | ||
|
||
// SendMail uses TLS connection to send the mail | ||
// The email is sent to all address in the toList, | ||
// the body should be of type bytes, not strings | ||
// This returns error if any occurred. | ||
err := smtp.SendMail(host+":"+port, auth, from, toList, body) | ||
|
||
// handling the errors | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/reiver/go-telnet" | ||
"net" | ||
) | ||
|
||
func main() { | ||
RunTelnetServer() | ||
} | ||
|
||
var ( | ||
Port = 5555 | ||
//Listener net.Listener | ||
) | ||
|
||
type Server telnet.Server | ||
|
||
func handler(port int) telnet.Handler { | ||
return telnet.EchoHandler | ||
} | ||
|
||
func RunTelnetServer() error { | ||
server := &Server{Addr: fmt.Sprintf(":%v", Port), Handler: handler(Port)} | ||
err := server.MyListenAndServe() | ||
|
||
if nil != err { | ||
//@TODO: Handle this error better. | ||
panic(err) | ||
} | ||
return err | ||
} | ||
|
||
func StopTelnetServer(listener net.Listener) { | ||
fmt.Println("[Telnet] Shutting down ...") | ||
listener.Close() | ||
} | ||
|
||
func (server *Server) MyListenAndServe() error { | ||
|
||
addr := server.Addr | ||
if "" == addr { | ||
addr = ":telnet" | ||
} | ||
|
||
listener, err := net.Listen("tcp", addr) | ||
if nil != err { | ||
return err | ||
} | ||
|
||
//Listener = listener | ||
fmt.Println("[Telnet] Listening at port: ", Port) | ||
return telnet.Serve(listener, handler(Port)) | ||
} |
Oops, something went wrong.