github.com/devops-filetransfer/sshego@v7.0.4+incompatible/cmd/manual-unixdomain-client/client.go (about)

     1  /*
     2  a simple test client. Change the settings at the top of main()
     3  to manually test your setup.
     4  */
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  
    11  	"github.com/glycerine/sshego"
    12  )
    13  
    14  func main() {
    15  	// you'll have to set these to do
    16  	// manual testing. These are just guesses/to get you started.
    17  
    18  	home := os.Getenv("HOME")
    19  	user := os.Getenv("USER") // may need to be changed
    20  	sshd := "example.com"     // definitely must be changed.
    21  	target := "/tmp/test-manual-unixdomain-recv:8888"
    22  
    23  	// N.B. we will append any newly seen hosts to this file
    24  	// if addNewHost is true.
    25  	kh := home + "/.ssh/known_hosts"
    26  
    27  	rsaPath := home + "/.ssh/id_rsa" // definitely must be set, unlikely to be correct as is.
    28  	addNewHost := false
    29  
    30  	// done with settings
    31  	dc := sshego.DialConfig{
    32  		ClientKnownHostsPath: kh,
    33  		Mylogin:              user,
    34  		RsaPath:              rsaPath,
    35  		Sshdhost:             sshd,
    36  		Sshdport:             22,
    37  		DownstreamHostPort:   target,
    38  		TofuAddIfNotKnown:    addNewHost,
    39  	}
    40  
    41  	channelToTcpServer, _, err := dc.Dial()
    42  	panicOn(err)
    43  
    44  	confirmationPayload := "ping"
    45  	m, err := channelToTcpServer.Write([]byte(confirmationPayload))
    46  	panicOn(err)
    47  	if m != len(confirmationPayload) {
    48  		panic("too short a write!")
    49  	}
    50  
    51  	payloadByteCount := 4
    52  	confirmationReply := "pong"
    53  
    54  	// check reply
    55  	rep := make([]byte, payloadByteCount)
    56  	m, err = channelToTcpServer.Read(rep)
    57  	panicOn(err)
    58  	if m != payloadByteCount {
    59  		panic("too short a reply!")
    60  	}
    61  	srep := string(rep)
    62  	if srep != confirmationReply {
    63  		panic(fmt.Errorf("saw '%s' but expected '%s'", srep, confirmationReply))
    64  	}
    65  	fmt.Printf("reply success! we got the expected srep reply '%s'\n", srep)
    66  
    67  	channelToTcpServer.Close()
    68  }
    69  
    70  func panicOn(err error) {
    71  	if err != nil {
    72  		panic(err)
    73  	}
    74  }