本文整理匯總了Golang中code/google/com/p/go/crypto/ssh/terminal.NewTerminal函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewTerminal函數的具體用法?Golang NewTerminal怎麽用?Golang NewTerminal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewTerminal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Start
func (c *cliClient) Start() {
oldState, err := terminal.MakeRaw(0)
if err != nil {
panic(err.Error())
}
defer terminal.Restore(0, oldState)
signal.Notify(make(chan os.Signal), os.Interrupt)
wrapper, interruptChan := NewTerminalWrapper(os.Stdin)
wrapper.SetErrorOnInterrupt(true)
c.interrupt = interruptChan
c.termWrapper = wrapper
c.term = terminal.NewTerminal(wrapper, "> ")
if width, height, err := terminal.GetSize(0); err == nil {
c.term.SetSize(width, height)
}
c.loadUI()
if c.writerChan != nil {
c.save()
}
if c.writerChan != nil {
close(c.writerChan)
<-c.writerDone
}
if c.fetchNowChan != nil {
close(c.fetchNowChan)
}
if c.stateLock != nil {
c.stateLock.Close()
}
}
示例2: handleSshConnection
func handleSshConnection(sConn *ssh.ServerConn,
msgchan chan<- text.Message, addchan chan<- Client, rmchan chan<- net.Conn) {
defer sConn.Close()
for {
ch, err := sConn.Accept()
if err == io.EOF {
return
}
if err != nil {
log.Println("handleServerConn Accept:", err)
break
}
if ch.ChannelType() != "session" {
ch.Reject(ssh.UnknownChannelType, "unknown channel type")
break
}
log.Println("Client version:", string(sConn.ClientVersion))
// Create terminal
term := terminal.NewTerminal(ch, "")
serverTerm := &ssh.ServerTerminal{
Term: term,
Channel: ch,
}
ch.Accept()
go handleConnection(sConn, serverTerm, term, msgchan, addchan, rmchan)
}
}
示例3: newServerShell
func newServerShell(ch *serverChan, prompt string) *ServerTerminal {
term := terminal.NewTerminal(ch, prompt)
return &ServerTerminal{
Term: term,
Channel: ch,
}
}
示例4: newTerm
func newTerm() Term {
u := new(uterm)
var err error
u.s, err = terminal.MakeRaw(0)
if err != nil {
panic(err)
}
u.t = terminal.NewTerminal(os.Stdin, "lixian >> ")
return u
}
示例5: handleUser
func handleUser(sConn *ssh.ServerConn) {
if err := sConn.Handshake(); err != nil {
fmt.Printf("failed to handshake\n")
return
}
// A ServerConn multiplexes several channels, which must
// themselves be Accepted.
for {
// Accept reads from the connection, demultiplexes packets
// to their corresponding channels and returns when a new
// channel request is seen. Some goroutine must always be
// calling Accept; otherwise no messages will be forwarded
// to the channels.
channel, err := sConn.Accept()
if err != nil {
fmt.Printf("error from Accept\n")
return
}
// Channels have a type, depending on the application level
// protocol intended. In the case of a shell, the type is
// "session" and ServerShell may be used to present a simple
// terminal interface.
if channel.ChannelType() != "session" {
channel.Reject(ssh.UnknownChannelType, "unknown channel type")
continue
}
channel.Accept()
term := terminal.NewTerminal(channel, "> ")
serverTerm := &ssh.ServerTerminal{
Term: term,
Channel: channel,
}
go func() {
defer channel.Close()
for {
line, err := serverTerm.ReadLine()
if err != nil {
break
}
fmt.Println(line)
}
}()
}
}
示例6: HandleConnection
func HandleConnection(listener net.Listener) {
for {
nConn, err := listener.Accept()
go func() {
if err != nil {
panic("failed to accept incoming connection")
}
// Before use, a handshake must be performed on the incoming
// net.Conn.
_, chans, _, err := ssh.NewServerConn(nConn, config)
if err == io.EOF {
return
}
if err != nil {
logfile.Printf("Handshake error: %s", err)
}
for newChannel := range chans {
channel, requests, err := newChannel.Accept()
if err != nil {
logfile.Println("[fatal] could not accept channel.")
continue
}
var term *terminal.Terminal
term = terminal.NewTerminal(channel, "")
go HandleSshRequests(channel, requests, term)
logfile.Println("[channelType]: " + newChannel.ChannelType())
//newChannel.Reject(ssh.ConnectionFailed, "")
// Sessions have out-of-band requests such as "shell",
// "pty-req" and "env".
if newChannel.ChannelType() == "direct-tcpip" {
http_request := make(map[string]string)
HandleTcpReading(channel, term, http_request)
go SaveHttpRequest(http_request)
} else {
go HandleTerminalReading(channel, term)
}
}
}()
}
}
示例7: handleChannel
func handleChannel(ch ssh.Channel) {
term := terminal.NewTerminal(ch, "> ")
serverTerm := &ssh.ServerTerminal{
Term: term,
Channel: ch,
}
ch.Accept()
defer ch.Close()
for {
line, err := serverTerm.ReadLine()
if err == io.EOF {
return
}
if err != nil {
log.Println("handleChannel readLine err:", err)
continue
}
fmt.Println(line)
}
}
示例8: NewStartedQShell
// NewStartedQShell returns an initialized QShell, ready to Interact() with.
// Prints mongo startup message.
func NewStartedQShell(driver *strata.Driver, mountPath string, showSize bool, mongodPath, mongoPath string) (*QShell, error) {
finished := false
sh := QShell{driver: driver, mountPath: mountPath, showSize: showSize, mongodPath: mongodPath, mongoPath: mongoPath}
// Put terminal in raw mode
var err error
sh.stdinFd = int(os.Stdin.Fd())
sh.oldState, err = terminal.MakeRaw(sh.stdinFd)
if err != nil {
return &sh, err
}
// Convention seems to be that caller invokes Close() only if there is no error.
// But we need to clean up if there is an error.
defer func() {
if !finished {
sh.Close()
}
}()
sh.term = terminal.NewTerminal(&readerWriter{r: os.Stdin, w: os.Stdout}, "\r> ")
sh.mongoStateHolder, err = newMongoStateHolder(sh.mongoPath)
if err != nil {
return &sh, err
}
if err := sh.print("Wrapper around MongoDB shell with additional commands for querying backups.\n"); err != nil {
return &sh, err
}
if err := sh.printMongoOutput(true); err != nil {
return &sh, err
}
finished = true
return &sh, nil
}
示例9: main
func main() {
w := eval.NewWorld()
w.DefineVar("connect", eval.NewFuncType([]eval.Type{}, false, []eval.Type{}), &funcV{&testFunc{}})
fmt.Println(":: welcome to go-eval...\n(hit ^D to exit)")
fd := int(os.Stdin.Fd())
oldState, err := terminal.MakeRaw(fd)
if err != nil {
panic(err)
}
defer terminal.Restore(fd, oldState)
term := terminal.NewTerminal(&shell{r: os.Stdin, w: os.Stdout}, "> ")
if term == nil {
panic(errors.New("could not create terminal"))
}
for {
line, err := term.ReadLine()
if err != nil {
break
}
code, err := w.Compile(fset, line)
if err != nil {
term.Write([]byte(err.Error() + "\n"))
continue
}
v, err := code.Run()
if err != nil {
term.Write([]byte(err.Error() + "\n"))
continue
}
if v != nil {
term.Write([]byte(v.String() + "\n"))
}
}
}
示例10: ExampleNewServerConn
func ExampleNewServerConn() {
// An SSH server is represented by a ServerConfig, which holds
// certificate details and handles authentication of ServerConns.
config := &ServerConfig{
PasswordCallback: func(c ConnMetadata, pass []byte) (*Permissions, error) {
// Should use constant-time compare (or better, salt+hash) in
// a production setting.
if c.User() == "testuser" && string(pass) == "tiger" {
return nil, nil
}
return nil, fmt.Errorf("password rejected for %q", c.User())
},
}
privateBytes, err := ioutil.ReadFile("id_rsa")
if err != nil {
panic("Failed to load private key")
}
private, err := ParsePrivateKey(privateBytes)
if err != nil {
panic("Failed to parse private key")
}
config.AddHostKey(private)
// Once a ServerConfig has been configured, connections can be
// accepted.
listener, err := net.Listen("tcp", "0.0.0.0:2022")
if err != nil {
panic("failed to listen for connection")
}
nConn, err := listener.Accept()
if err != nil {
panic("failed to accept incoming connection")
}
// Before use, a handshake must be performed on the incoming
// net.Conn.
_, chans, reqs, err := NewServerConn(nConn, config)
if err != nil {
panic("failed to handshake")
}
// The incoming Request channel must be serviced.
go DiscardRequests(reqs)
// Service the incoming Channel channel.
for newChannel := range chans {
// Channels have a type, depending on the application level
// protocol intended. In the case of a shell, the type is
// "session" and ServerShell may be used to present a simple
// terminal interface.
if newChannel.ChannelType() != "session" {
newChannel.Reject(UnknownChannelType, "unknown channel type")
continue
}
channel, requests, err := newChannel.Accept()
if err != nil {
panic("could not accept channel.")
}
// Sessions have out-of-band requests such as "shell",
// "pty-req" and "env". Here we handle only the
// "shell" request.
go func(in <-chan *Request) {
for req := range in {
ok := false
switch req.Type {
case "shell":
ok = true
if len(req.Payload) > 0 {
// We don't accept any
// commands, only the
// default shell.
ok = false
}
}
req.Reply(ok, nil)
}
}(requests)
term := terminal.NewTerminal(channel, "> ")
go func() {
defer channel.Close()
for {
line, err := term.ReadLine()
if err != nil {
break
}
fmt.Println(line)
}
}()
}
}
示例11: main
func main() {
// An SSH server is represented by a ServerConfig, which holds
// certificate details and handles authentication of ServerConns.
config := &ssh.ServerConfig{
PasswordCallback: func(conn *ssh.ServerConn, user, pass string) bool {
return user == "testuser" && pass == "tiger"
},
}
privateBytes, err := ioutil.ReadFile("id_rsa")
if err != nil {
panic("Failed to load private key")
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
panic("Failed to parse private key")
}
config.AddHostKey(private)
// Once a ServerConfig has been configured, connections can be
// accepted.
listener, err := ssh.Listen("tcp", "0.0.0.0:2022", config)
if err != nil {
panic("failed to listen for connection")
}
sConn, err := listener.Accept()
if err != nil {
panic("failed to accept incoming connection")
}
if err := sConn.Handshake(); err != nil {
panic("failed to handshake")
}
// A ServerConn multiplexes several channels, which must
// themselves be Accepted.
for {
// Accept reads from the connection, demultiplexes packets
// to their corresponding channels and returns when a new
// channel request is seen. Some goroutine must always be
// calling Accept; otherwise no messages will be forwarded
// to the channels.
channel, err := sConn.Accept()
if err != nil {
panic("error from Accept")
}
// Channels have a type, depending on the application level
// protocol intended. In the case of a shell, the type is
// "session" and ServerShell may be used to present a simple
// terminal interface.
if channel.ChannelType() != "session" {
channel.Reject(ssh.UnknownChannelType, "unknown channel type")
continue
}
channel.Accept()
term := terminal.NewTerminal(channel, "> ")
serverTerm := &ssh.ServerTerminal{
Term: term,
Channel: channel,
}
go func() {
defer channel.Close()
for {
line, err := serverTerm.ReadLine()
if err != nil {
break
}
fmt.Println(line)
}
}()
}
}
示例12: main
func main() {
println("starting ssh server...")
err := envconfig.Process("wormhole", &appConfig)
if err != nil {
log.Fatal(err.Error())
}
if appConfig.LocalSSHAddr == "" || appConfig.PrivateKeyPath == "" ||
appConfig.RemoteSSHAddr == "" || appConfig.RemoteForwardAddress == "" ||
appConfig.RemoteSSHUser == "" || appConfig.RemotePrivateKeyPath == "" {
fmt.Println("Missing config")
os.Exit(-1)
}
// An SSH server is represented by a ServerConfig, which holds
// certificate details and handles authentication of ServerConns.
config := &ssh.ServerConfig{
PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
// Should use constant-time compare (or better, salt+hash) in
// a production setting.
if c.User() == "testuser" && string(pass) == "" {
return nil, nil
}
return nil, fmt.Errorf("password rejected for %q", c.User())
},
}
privateBytes, err := ioutil.ReadFile(appConfig.PrivateKeyPath)
if err != nil {
panic("Failed to load private key")
}
private, err := ssh.ParsePrivateKey(privateBytes)
if err != nil {
panic("Failed to parse private key")
}
config.AddHostKey(private)
// Once a ServerConfig has been configured, connections can be
// accepted.
listener, err := net.Listen("tcp", appConfig.LocalSSHAddr)
if err != nil {
panic("failed to listen for connection")
}
for {
nConn, err := listener.Accept()
if err != nil {
panic("failed to accept incoming connection")
}
conn, chans, reqs, err := ssh.NewServerConn(nConn, config)
if err != nil {
panic("failed to handshake")
}
go processRequests(conn, reqs)
for newChannel := range chans {
if newChannel.ChannelType() != "session" {
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
continue
}
channel, requests, err := newChannel.Accept()
if err != nil {
panic("could not accept channel.")
}
go func(in <-chan *ssh.Request) {
for req := range in {
ok := false
switch req.Type {
case "shell":
ok = true
if len(req.Payload) > 0 {
ok = false
}
}
req.Reply(ok, nil)
}
}(requests)
term := terminal.NewTerminal(channel, "> ")
go func() {
defer channel.Close()
for {
_, err := term.ReadLine()
if err != nil {
break
}
}
}()
}
}
}
示例13: newServerShell
func newServerShell(ch Channel, in <-chan *Request, prompt string) *terminal.Terminal {
term := terminal.NewTerminal(ch, prompt)
go handleTerminalRequests(in)
return term
}
示例14: main
func main() {
flag.Parse()
oldState, err := terminal.MakeRaw(0)
if err != nil {
panic(err.Error())
}
defer terminal.Restore(0, oldState)
term := terminal.NewTerminal(os.Stdin, "> ")
updateTerminalSize(term)
resizeChan := make(chan os.Signal)
go func() {
for _ = range resizeChan {
updateTerminalSize(term)
}
}()
signal.Notify(resizeChan, syscall.SIGWINCH)
if len(*configFile) == 0 {
homeDir := os.Getenv("HOME")
if len(homeDir) == 0 {
alert(term, "$HOME not set. Please either export $HOME or use the -config-file option.\n")
return
}
persistentDir := filepath.Join(homeDir, "Persistent")
if stat, err := os.Lstat(persistentDir); err == nil && stat.IsDir() {
// Looks like Tails.
homeDir = persistentDir
}
*configFile = filepath.Join(homeDir, ".xmpp-client")
}
config, err := ParseConfig(*configFile)
if err != nil {
alert(term, "Failed to parse config file: "+err.Error())
config = new(Config)
if !enroll(config, term) {
return
}
config.filename = *configFile
config.Save()
}
password := config.Password
if len(password) == 0 {
if password, err = term.ReadPassword(fmt.Sprintf("Password for %s (will not be saved to disk): ", config.Account)); err != nil {
alert(term, "Failed to read password: "+err.Error())
return
}
}
parts := strings.SplitN(config.Account, "@", 2)
if len(parts) != 2 {
alert(term, "invalid username (want [email protected]): "+config.Account)
return
}
user := parts[0]
domain := parts[1]
var addr string
addrTrusted := false
if len(config.Server) > 0 && config.Port > 0 {
addr = fmt.Sprintf("%s:%d", config.Server, config.Port)
addrTrusted = true
} else {
if len(config.Proxies) > 0 {
alert(term, "Cannot connect via a proxy without Server and Port being set in the config file as an SRV lookup would leak information.")
return
}
host, port, err := xmpp.Resolve(domain)
if err != nil {
alert(term, "Failed to resolve XMPP server: "+err.Error())
return
}
addr = fmt.Sprintf("%s:%d", host, port)
}
var dialer proxy.Dialer
for i := len(config.Proxies) - 1; i >= 0; i-- {
u, err := url.Parse(config.Proxies[i])
if err != nil {
alert(term, "Failed to parse "+config.Proxies[i]+" as a URL: "+err.Error())
return
}
if dialer == nil {
dialer = proxy.Direct
}
if dialer, err = proxy.FromURL(u, dialer); err != nil {
alert(term, "Failed to parse "+config.Proxies[i]+" as a proxy: "+err.Error())
return
}
}
var certSHA256 []byte
if len(config.ServerCertificateSHA256) > 0 {
certSHA256, err = hex.DecodeString(config.ServerCertificateSHA256)
if err != nil {
alert(term, "Failed to parse ServerCertificateSHA256 (should be hex string): "+err.Error())
//.........這裏部分代碼省略.........
示例15: run
func run() error {
fset := twik.NewFileSet()
scope := twik.NewDefaultScope(fset)
scope.Create("printf", printfFn)
scope.Create("list", listFn)
if len(os.Args) > 1 {
if strings.HasPrefix(os.Args[1], "-") {
return fmt.Errorf("usage: twik [<source file>]")
}
f, err := os.Open(os.Args[1])
if err != nil {
return err
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return err
}
node, err := twik.Parse(fset, os.Args[1], data)
if err != nil {
return err
}
_, err = scope.Eval(node)
return err
}
state, err := terminal.MakeRaw(1)
if err != nil {
return err
}
defer terminal.Restore(1, state)
t := terminal.NewTerminal(os.Stdout, "> ")
unclosed := ""
for {
line, err := t.ReadLine()
if err == io.EOF {
break
}
if err != nil {
return err
}
if unclosed != "" {
line = unclosed + "\n" + line
}
unclosed = ""
t.SetPrompt("> ")
node, err := twik.ParseString(fset, "", line)
if err != nil {
if strings.HasSuffix(err.Error(), "missing )") {
unclosed = line
t.SetPrompt(". ")
continue
}
fmt.Println(err)
continue
}
value, err := scope.Eval(node)
if err != nil {
fmt.Println(err)
continue
}
if value != nil {
if reflect.TypeOf(value).Kind() == reflect.Func {
fmt.Println("#func")
} else if v, ok := value.([]interface{}); ok {
if len(v) == 0 {
fmt.Println("()")
} else {
fmt.Print("(list")
for _, e := range v {
fmt.Printf(" %#v", e)
}
fmt.Println(")")
}
} else {
fmt.Printf("%#v\n", value)
}
}
}
fmt.Println()
return nil
}