-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c55adf4
commit 2bd413c
Showing
8 changed files
with
330 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
package vpn | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net" | ||
"sync" | ||
|
||
"github.com/labstack/echo/v4" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/songgao/water" | ||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
// InterfaceName defines the default name for the network interface created by ShellHub. | ||
const InterfaceName string = "shb" | ||
|
||
var ErrGenerateInterface = errors.New("failed to generate an interface") | ||
|
||
func GenerateInterfaceName() (string, error) { | ||
const Attempts = 10 | ||
|
||
for i := 0; i < Attempts; i++ { | ||
name := fmt.Sprintf("%s%d", InterfaceName, i) | ||
|
||
if _, err := netlink.LinkByName(name); err == nil { | ||
continue | ||
} | ||
|
||
return name, nil | ||
} | ||
|
||
return "", ErrGenerateInterface | ||
} | ||
|
||
type Settings struct { | ||
Address [4]byte `json:"address"` | ||
Mask byte `json:"mask"` | ||
} | ||
|
||
// ParseSettings read and parses the [Settings] structure from an [io.Reader]. | ||
func ParseSettings(data io.Reader) (*Settings, error) { | ||
body, err := io.ReadAll(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
settings := Settings{} | ||
if err = json.Unmarshal(body, &settings); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &settings, nil | ||
} | ||
|
||
// String converts a [Settings] to a string representation on the format $IP/$Mask. | ||
func (s *Settings) String() string { | ||
ip := net.IPv4(s.Address[0], s.Address[1], s.Address[2], s.Address[3]) | ||
|
||
return fmt.Sprintf("%s/%d", ip.String(), s.Mask) | ||
} | ||
|
||
func connHandler() func(c echo.Context) error { | ||
return func(c echo.Context) error { | ||
log.Info("new connection") | ||
|
||
conn, _, err := c.Response().Hijack() | ||
if err != nil { | ||
log.Error(err) | ||
|
||
return err | ||
} | ||
|
||
settings, err := ParseSettings(c.Request().Body) | ||
if err != nil { | ||
log.Error(err) | ||
|
||
return err | ||
} | ||
|
||
log.WithFields(log.Fields{ | ||
"addrss": settings.Address, | ||
"mask": settings.Mask, | ||
}).Info("handshake data") | ||
|
||
name, err := GenerateInterfaceName() | ||
if err != nil { | ||
log.Error(err) | ||
|
||
return err | ||
} | ||
|
||
ifce, err := water.New(water.Config{ | ||
DeviceType: water.TUN, | ||
PlatformSpecificParams: water.PlatformSpecificParams{ | ||
Name: name, | ||
}, | ||
}) | ||
if err != nil { | ||
log.Error(err) | ||
|
||
return err | ||
} | ||
|
||
defer ifce.Close() | ||
|
||
log.WithFields(log.Fields{ | ||
"name": ifce.Name(), | ||
}).Info("interface created") | ||
|
||
addr, err := netlink.ParseAddr(settings.String()) // "10.0.0.1/24") | ||
if err != nil { | ||
log.Error(err) | ||
|
||
return err | ||
} | ||
|
||
link, err := netlink.LinkByName(ifce.Name()) | ||
if err != nil { | ||
log.Error(err) | ||
|
||
return err | ||
} | ||
|
||
if err := netlink.AddrAdd(link, addr); err != nil { | ||
log.Error(err) | ||
|
||
return err | ||
} | ||
|
||
log.Info("interface configured") | ||
|
||
if err := netlink.LinkSetUp(link); err != nil { | ||
log.Error(err) | ||
|
||
return err | ||
} | ||
|
||
log.Info("interface up") | ||
|
||
wg := new(sync.WaitGroup) | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
|
||
buffer := make([]byte, 2048) | ||
|
||
for { | ||
log.Trace("reading from interface") | ||
|
||
read, err := ifce.Read(buffer) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Trace("writing to connection") | ||
|
||
if _, err = conn.Write(buffer[:read]); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
}() | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
|
||
buffer := make([]byte, 2048) | ||
|
||
for { | ||
log.Trace("reading from connetion") | ||
|
||
read, err := conn.Read(buffer) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if read == 0 { | ||
log.Debug("zero bytes was read from the connetion") | ||
|
||
continue | ||
} | ||
|
||
log.Trace("writing to interface") | ||
|
||
if _, err = ifce.Write(buffer[:read]); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
}() | ||
|
||
wg.Wait() | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func closeHandler() func(c echo.Context) error { | ||
return func(c echo.Context) error { | ||
log.Trace("close handler called") | ||
|
||
return nil | ||
} | ||
} |
Oops, something went wrong.