direct communication channel now setup between agent and converge server
This commit is contained in:
		
							parent
							
								
									20ce37a1cb
								
							
						
					
					
						commit
						5bd3795534
					
				| @ -4,6 +4,7 @@ import ( | ||||
| 	"fmt" | ||||
| 	"github.com/hashicorp/yamux" | ||||
| 	"io" | ||||
| 	"log" | ||||
| 	"net" | ||||
| ) | ||||
| 
 | ||||
| @ -20,25 +21,49 @@ const ( | ||||
| ) | ||||
| 
 | ||||
| func NewCommChannel(role Role, wsConn io.ReadWriteCloser) (CommChannel, error) { | ||||
| 	var commChannel CommChannel | ||||
| 	switch role { | ||||
| 	case Agent: | ||||
| 		listener, err := yamux.Server(wsConn, nil) | ||||
| 		if err != nil { | ||||
| 			return CommChannel{}, err | ||||
| 		} | ||||
| 		return CommChannel{ | ||||
| 		commChannel = CommChannel{ | ||||
| 			Peer:    nil, | ||||
| 			Session: listener, | ||||
| 		}, nil | ||||
| 		} | ||||
| 	case ConvergeServer: | ||||
| 		clientSession, err := yamux.Client(wsConn, nil) | ||||
| 		if err != nil { | ||||
| 			return CommChannel{}, err | ||||
| 		} | ||||
| 		return CommChannel{ | ||||
| 		commChannel = CommChannel{ | ||||
| 			Peer:    nil, | ||||
| 			Session: clientSession, | ||||
| 		}, nil | ||||
| 		} | ||||
| 	default: | ||||
| 		panic(fmt.Errorf("Undefined role %d", role)) | ||||
| 	} | ||||
| 	return CommChannel{}, fmt.Errorf("Undefined role %d", role) | ||||
| 
 | ||||
| 	// communication from Agent to ConvergeServer
 | ||||
| 
 | ||||
| 	switch role { | ||||
| 	case Agent: | ||||
| 		conn, err := commChannel.Session.OpenStream() | ||||
| 		commChannel.Peer = conn | ||||
| 		if err != nil { | ||||
| 			return CommChannel{}, err | ||||
| 		} | ||||
| 	case ConvergeServer: | ||||
| 		conn, err := commChannel.Session.Accept() | ||||
| 		commChannel.Peer = conn | ||||
| 		if err != nil { | ||||
| 			return CommChannel{}, err | ||||
| 		} | ||||
| 	default: | ||||
| 		panic(fmt.Errorf("Undefined role %d", role)) | ||||
| 	} | ||||
| 
 | ||||
| 	log.Println("Communication channel between agent and converge server established") | ||||
| 	return commChannel, nil | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user