go-telnet
所属分类:Telnet服务器
开发工具:GO
文件大小:0KB
下载次数:0
上传日期:2022-11-22 05:14:07
上 传 者:
sh-1993
说明: telnet客户端服务器,
(telnet client server,)
文件列表:
LICENSE (1112, 2022-11-21)
caller.go (619, 2022-11-21)
client.go (1484, 2022-11-21)
conn.go (3451, 2022-11-21)
context.go (381, 2022-11-21)
data_reader.go (3427, 2022-11-21)
data_reader_test.go (9490, 2022-11-21)
data_writer.go (3537, 2022-11-21)
data_writer_test.go (2459, 2022-11-21)
discard_logger.go (809, 2022-11-21)
doc.go (13528, 2022-11-21)
echo_handler.go (641, 2022-11-21)
echo_handler_test.go (9092, 2022-11-21)
go.mod (84, 2022-11-21)
go.sum (165, 2022-11-21)
handler.go (602, 2022-11-21)
logger.go (263, 2022-11-21)
reader.go (69, 2022-11-21)
server.go (4167, 2022-11-21)
standard_caller.go (2013, 2022-11-21)
standard_caller_test.go (24919, 2022-11-21)
telsh/ (0, 2022-11-21)
telsh/discard_logger.go (808, 2022-11-21)
telsh/doc.go (3474, 2022-11-21)
telsh/handler.go (3207, 2022-11-21)
telsh/help.go (2253, 2022-11-21)
telsh/producer.go (971, 2022-11-21)
telsh/telnet_handler.go (6733, 2022-11-21)
telsh/telnet_handler_test.go (1790, 2022-11-21)
tls.go (3525, 2022-11-21)
writer.go (70, 2022-11-21)
# go-telnet
Package **telnet** provides TELNET and TELNETS client and server implementations, for the Go programming language.
The **telnet** package provides an API in a style similar to the "net/http" library that is part of the Go standard library, including support for "middleware".
(TELNETS is *secure TELNET*, with the TELNET protocol over a secured TLS (or SSL) connection.)
## Fork
Note that the original repo at https//github.com/reiver/go-telnet seems to be abandoned. This repo intends to pick up the pending fixes and at least try to maintain it.
## Documention
Online documentation, which includes examples, can be found at: http://godoc.org/github.com/matjam/go-telnet
[![GoDoc](https://godoc.org/github.com/matjam/go-telnet?status.svg)](https://godoc.org/github.com/matjam/go-telnet)
## Very Simple TELNET Server Example
A very very simple TELNET server is shown in the following code.
This particular TELNET server just echos back to the user anything they "submit" to the server.
(By default, a TELNET client does *not* send anything to the server until the [Enter] key is pressed.
"Submit" means typing something and then pressing the [Enter] key.)
```
package main
import (
"github.com/reiver/go-telnet"
)
func main() {
var handler telnet.Handler = telnet.EchoHandler
err := telnet.ListenAndServe(":5555", handler)
if nil != err {
//@TODO: Handle this error better.
panic(err)
}
}
```
If you wanted to test out this very very simple TELNET server, if you were on the same computer it was
running, you could connect to it using the bash command:
```
telnet localhost 5555
```
(Note that we use the same TCP port number -- "5555" -- as we had in our code. That is important, as the
value used by your TELNET server and the value used by your TELNET client **must** match.)
## Very Simple (Secure) TELNETS Server Example
TELNETS is the secure version of TELNET.
The code to make a TELNETS server is very similar to the code to make a TELNET server.
(The difference between we use the `telnet.ListenAndServeTLS` func instead of the
`telnet.ListenAndServe` func.)
```
package main
import (
"github.com/reiver/go-telnet"
)
func main() {
var handler telnet.Handler = telnet.EchoHandler
err := telnet.ListenAndServeTLS(":5555", "cert.pem", "key.pem", handler)
if nil != err {
//@TODO: Handle this error better.
panic(err)
}
}
```
If you wanted to test out this very very simple TELNETS server, get the `telnets` client program from here:
https://github.com/reiver/telnets
## TELNET Client Example:
```
package main
import (
"github.com/matjam/go-telnet"
)
func main() {
var caller telnet.Caller = telnet.StandardCaller
//@TOOD: replace "example.net:5555" with address you want to connect to.
telnet.DialToAndCall("example.net:5555", caller)
}
```
## TELNETS Client Example:
```
package main
import (
"github.com/reiver/go-telnet"
"crypto/tls"
)
func main() {
//@TODO: Configure the TLS connection here, if you need to.
tlsConfig := &tls.Config{}
var caller telnet.Caller = telnet.StandardCaller
//@TOOD: replace "example.net:5555" with address you want to connect to.
telnet.DialToAndCallTLS("example.net:5555", caller, tlsConfig)
}
```
## TELNET Shell Server Example
A more useful TELNET servers can be made using the `"github.com/matjam/go-telnet/telsh"` sub-package.
For example:
```
package main
import (
"github.com/reiver/go-oi"
"github.com/reiver/go-telnet"
"github.com/reiver/go-telnet/telsh"
"io"
"time"
)
func fiveHandler(stdin io.ReadCloser, stdout io.WriteCloser, stderr io.WriteCloser, args ...string) error {
oi.LongWriteString(stdout, "The number FIVE looks like this: 5\r\n")
return nil
}
func fiveProducer(ctx telnet.Context, name string, args ...string) telsh.Handler{
return telsh.PromoteHandlerFunc(fiveHandler)
}
func danceHandler(stdin io.ReadCloser, stdout io.WriteCloser, stderr io.WriteCloser, args ...string) error {
for i:=0; i<20; i++ {
oi.LongWriteString(stdout, "\r")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r")
time.Sleep(50*time.Millisecond)
oi.LongWriteString(stdout, "\r")
time.Sleep(50*time.Millisecond)
}
oi.LongWriteString(stdout, "\r \r\n")
return nil
}
func danceProducer(ctx telnet.Context, name string, args ...string) telsh.Handler{
return telsh.PromoteHandlerFunc(danceHandler)
}
func main() {
shellHandler := telsh.NewShellHandler()
shellHandler.WelcomeMessage = `
__ __ ______ _ _____ ____ __ __ ______
\ \ / /| ____|| | / ____| / __ \ | \/ || ____|
\ \ /\ / / | |__ | | | | | | | || \ / || |__
\ \/ \/ / | __| | | | | | | | || |\/| || __|
\ /\ / | |____ | |____ | |____ | |__| || | | || |____
\/ \/ |______||______| \_____| \____/ |_| |_||______|
`
// Register the "five" command.
commandName := "five"
commandProducer := telsh.ProducerFunc(fiveProducer)
shellHandler.Register(commandName, commandProducer)
// Register the "dance" command.
commandName = "dance"
commandProducer = telsh.ProducerFunc(danceProducer)
shellHandler.Register(commandName, commandProducer)
shellHandler.Register("dance", telsh.ProducerFunc(danceProducer))
addr := ":5555"
if err := telnet.ListenAndServe(addr, shellHandler); nil != err {
panic(err)
}
}
```
TELNET servers made using the `"github.com/matjam/go-telnet/telsh"` sub-package will often be more useful
as it makes it easier for you to create a *shell* interface.
# More Information
There is a lot more information about documentation on all this here: http://godoc.org/github.com/matjam/go-telnet
(You should really read those.)
近期下载者:
相关文件:
收藏者: