Created
February 25, 2021 03:44
-
-
Save ihipop/fc127c0bec58fb35dbb3ba38f9e6ee92 to your computer and use it in GitHub Desktop.
Fake Telnet Server
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
package main | |
import ( | |
"fmt" | |
"github.com/reiver/go-oi" | |
"github.com/reiver/go-telnet" | |
) | |
func main() { | |
var stdLogger telnet.Logger= &StdLogger{} | |
stdLogger.Debug("A fake Telnet Server By [email protected]") | |
stdLogger.Debug("Wait for new connection on 0.0.0.0:23") | |
server := &telnet.Server{ | |
Addr: "0.0.0.0:23", | |
Handler: FakeLoginHandler{}, | |
Logger: stdLogger, | |
} | |
err := server.ListenAndServe() | |
if nil != err { | |
//@TODO: Handle this error better. | |
panic(err) | |
} | |
} | |
type StdLogger struct{} | |
func (*StdLogger) Debug(args ...interface{}) { | |
fmt.Println(args...) | |
} | |
func (*StdLogger) Debugf(format string, args ...interface{}) { | |
fmt.Printf(format,args...); | |
fmt.Println("") | |
} | |
func (*StdLogger) Error(args ...interface{}) { | |
fmt.Println(args...) | |
} | |
func (StdLogger) Errorf(format string, args ...interface{}) { | |
fmt.Printf(format,args...) | |
fmt.Println("") | |
} | |
func (*StdLogger) Trace(args ...interface{}) { | |
fmt.Println(args...) | |
} | |
func (*StdLogger) Tracef(format string, args ...interface{}) { | |
fmt.Printf(format,args...) | |
fmt.Println("") | |
} | |
func (*StdLogger) Warn(args ...interface{}) { | |
fmt.Println(args...) | |
} | |
func (*StdLogger) Warnf(format string, args ...interface{}) { | |
fmt.Printf(format,args...) | |
fmt.Println("") | |
} | |
type FakeLoginHandler struct{} | |
func (handler FakeLoginHandler) ServeTELNET(ctx telnet.Context, w telnet.Writer, r telnet.Reader) { | |
var receivedLine string | |
var buffer [1]byte // Seems like the length of the buffer needs to be small, otherwise will have to wait for buffer to fill up. | |
p := buffer[:] | |
ctx.Logger().Debug("started new login connection") | |
_, _ = oi.LongWrite(w, []byte("stb login:")) | |
step := "username" | |
for { | |
n, err := r.Read(p) | |
if n > 0 { | |
if string(p[:n]) == "\n" { | |
reply := "Received "+step+": "+receivedLine | |
_, _ = oi.LongWriteString(w, reply+"\n") | |
ctx.Logger().Debug(reply) | |
receivedLine = "" | |
if step == "username" { | |
step = "password" | |
_, _ = oi.LongWrite(w, []byte("Password:")) | |
} else if step == "password" { | |
step = "text" | |
_, _ = oi.LongWrite(w, []byte("Login Success! Welcome!"+"\n")) | |
} | |
} else { | |
receivedLine += string(p[:n]) | |
} | |
// if nextstep == "" { | |
// oi.LongWrite(w, p[:n]) | |
// } | |
} | |
if nil != err { | |
ctx.Logger().Error(err) | |
break | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment