Skip to content

Commit

Permalink
feat(): make it possible to use bore as a module
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCyjaneK committed Aug 6, 2021
1 parent 92498fe commit a281b91
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
20 changes: 20 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import (
"bufio"
"encoding/json"
"fmt"
"io"
"net"
Expand All @@ -20,12 +22,19 @@ type BoreClient struct {
ServerEndpoint endpoint // remote SSH server
RemoteEndpoint endpoint // remote forwarding port (on remote SSH server network)
id string
RemoteData Data
}

type idRequestPayload struct {
ID string
}

type Data struct {
HTTPurl string `json:"httpurl"`
HTTPSurl string `json:"httpsurl"`
DirectTCP string `json:"directtcp"`
}

// NewBoreClient returns new instance of BoreClient.
func NewBoreClient(config Config) BoreClient {
return BoreClient{
Expand Down Expand Up @@ -109,6 +118,17 @@ func (c *BoreClient) writeStdout() error {
}

go func() {
s := bufio.NewScanner(stdout)
s.Scan()
text := s.Text()
err = json.Unmarshal([]byte(text), &c.RemoteData)
if err != nil {
os.Stdout.WriteString(text + "\n")
} else {
os.Stdout.WriteString("Generated HTTP URL: " + c.RemoteData.HTTPurl + "\n")
os.Stdout.WriteString("Generated HTTPS URL: " + c.RemoteData.HTTPSurl + "\n")
os.Stdout.WriteString("Direct TCP: " + c.RemoteData.DirectTCP + "\n")
}
defer session.Close()
io.Copy(os.Stdout, stdout)
}()
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *BoreServer) getHandler(handler http.Handler) http.Handler {

userID := strings.Split(r.Host, ".")[0]
if client, ok := s.sshServer.clients[userID]; ok {
client.write(fmt.Sprintf("%s\n", log))
client.write(log)
}
})
}
Expand Down
19 changes: 14 additions & 5 deletions server/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package server

import (
"crypto/rand"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"sync"
"time"

c "github.com/jkuri/bore/client"
"go.uber.org/zap"
"golang.org/x/crypto/ssh"
)
Expand Down Expand Up @@ -43,9 +45,14 @@ type client struct {
port uint32
}

func (c *client) write(data string) {
func (c *client) write(data interface{}) {
if c.ch != nil {
io.WriteString(c.ch, data)
b, err := json.Marshal(data)
if err != nil {
io.WriteString(c.ch, data.(string)+"\n")
return
}
io.WriteString(c.ch, string(b)+"\n")
}
}

Expand Down Expand Up @@ -205,10 +212,12 @@ func (s *SSHServer) handleRequests(client *client, reqs <-chan *ssh.Request) {
client.addr = bindInfo.Addr
client.port = bindInfo.Port

client.write(fmt.Sprintf("Generated HTTP URL: http://%s.%s\n", client.id, s.domain))
client.write(fmt.Sprintf("Generated HTTPS URL: https://%s.%s\n", client.id, s.domain))
client.write(fmt.Sprintf("Direct TCP: tcp://%s:%d\n\n", s.domain, client.port))
var data c.Data

data.HTTPurl = fmt.Sprintf("http://%s.%s", client.id, s.domain)
data.HTTPSurl = fmt.Sprintf("https://%s.%s", client.id, s.domain)
data.DirectTCP = fmt.Sprintf("tcp://%s:%d", s.domain, client.port)
client.write(data)
client.mu.Lock()
client.listeners[bindInfo.Bound] = listener
client.mu.Unlock()
Expand Down

0 comments on commit a281b91

Please sign in to comment.