Openstatus www.openstatus.dev
at 4c0f4c00a38753a5d0dfd7e7b7b7706dec6f1503 122 lines 2.9 kB view raw
1package main 2 3import ( 4 _ "embed" 5 "encoding/json" 6 "fmt" 7 "io" 8 "log" 9 "net/http" 10 11 "github.com/gliderlabs/ssh" 12) 13 14//go:embed banner.txt 15var banner string 16 17func bannerfunc(ctx ssh.Context) string { 18 return banner 19} 20 21var statusOk = ` 22+----------------------------------+ 23| | 24| All Systems Operational | 25| | 26+----------------------------------+ 27` 28var statusDegraded = ` 29+----------------------------------+ 30| | 31| System is degraded | 32| | 33+----------------------------------+ 34` 35var statusPartialOutage = ` 36+----------------------------------+ 37| | 38| System is partially out of | 39| service | 40| | 41+----------------------------------+ 42` 43var statusMajorOutage = ` 44+----------------------------------+ 45| | 46| System is out of service | 47| | 48+----------------------------------+ 49` 50var statusUnderMaintenance = ` 51+----------------------------------+ 52| | 53| System is under | 54| maintenance | 55| | 56+----------------------------------+ 57` 58var statusIncident = ` 59+----------------------------------+ 60| | 61| System is partially out of | 62| service | 63| | 64+----------------------------------+ 65` 66 67type status struct { 68 Status string `json:"status"` 69} 70 71func handler(s ssh.Session) { 72 url := fmt.Sprintf("https://api.openstatus.dev/public/status/%s", s.User()) 73 res, err := http.Get(url) 74 if err != nil { 75 fmt.Fprintf(s, "Error fetching status: %v\n", err) 76 return 77 } 78 defer res.Body.Close() 79 var status status 80 json.NewDecoder(res.Body).Decode(&status) 81 82 var currentStatus string 83 switch status.Status { 84 case "operational": 85 currentStatus = statusOk 86 case "degraded_performance": 87 currentStatus = statusDegraded 88 case "partial_outage": 89 currentStatus = statusPartialOutage 90 case "major_outage": 91 currentStatus = statusMajorOutage 92 case "under_maintenance": 93 currentStatus = statusUnderMaintenance 94 case "incident": 95 currentStatus = statusIncident 96 default: 97 currentStatus = "" 98 } 99 100 if currentStatus == "" { 101 io.WriteString(s, "Unknown status page") 102 return 103 } 104 105 io.WriteString(s, fmt.Sprintf("\nCurrent Status for: %s\n\n%s\n\nVisit the status page at https://%s.openstatus.dev/\n\n", s.User(), currentStatus, s.User())) 106} 107 108func main() { 109 110 server := &ssh.Server{ 111 Addr: ":2222", 112 BannerHandler: bannerfunc, 113 Handler: handler, 114 115 } 116 ssh.HostKeyFile("/data/id_rsa") 117 118 log.Println("starting ssh server on port 2222...") 119 log.Fatal(server.ListenAndServe()) 120 121 122}