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 9b71f04
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
15 changes: 15 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package client

import (
"bufio"
"encoding/json"
"fmt"
"io"
"net"
"os"
"os/signal"
"time"

"github.com/jkuri/bore/server"
"golang.org/x/crypto/ssh"
)

Expand All @@ -20,6 +23,7 @@ type BoreClient struct {
ServerEndpoint endpoint // remote SSH server
RemoteEndpoint endpoint // remote forwarding port (on remote SSH server network)
id string
RemoteData server.Data
}

type idRequestPayload struct {
Expand Down Expand Up @@ -109,6 +113,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
24 changes: 19 additions & 5 deletions server/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

import (
"crypto/rand"
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand All @@ -18,6 +19,12 @@ const (
maxPort = 65000
)

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

// SSHServer defines SSH server instance.
type SSHServer struct {
mu sync.Mutex
Expand All @@ -43,9 +50,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 +217,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 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 9b71f04

Please sign in to comment.