made it easier to send multiple messages with the same header
This commit is contained in:
parent
663caa5cc7
commit
99676ef4bf
|
@ -31,7 +31,7 @@ func (l *localConns) Write(p *packer.Packer) error {
|
||||||
l.lock.RLock()
|
l.lock.RLock()
|
||||||
defer l.lock.RUnlock()
|
defer l.lock.RUnlock()
|
||||||
|
|
||||||
key := fmt.Sprintf("%s:%d", p.Header.Address(), p.Header.Port)
|
key := fmt.Sprintf("%s:%d", p.Address(), p.Port())
|
||||||
if conn := l.locals[key]; conn != nil {
|
if conn := l.locals[key]; conn != nil {
|
||||||
_, err := conn.Write(p.Data.Data())
|
_, err := conn.Write(p.Data.Data())
|
||||||
return err
|
return err
|
||||||
|
@ -42,8 +42,8 @@ func (l *localConns) Write(p *packer.Packer) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *localConns) startConnection(orig *packer.Packer) {
|
func (l *localConns) startConnection(orig *packer.Packer) {
|
||||||
key := fmt.Sprintf("%s:%d", orig.Header.Address(), orig.Header.Port)
|
key := fmt.Sprintf("%s:%d", orig.Address(), orig.Port())
|
||||||
addr := fmt.Sprintf("127.0.0.1:%d", l.services[orig.Header.Service])
|
addr := fmt.Sprintf("127.0.0.1:%d", l.services[orig.Service()])
|
||||||
conn, err := net.Dial("tcp", addr)
|
conn, err := net.Dial("tcp", addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
loginfo.Println("failed to open connection to", addr, err)
|
loginfo.Println("failed to open connection to", addr, err)
|
||||||
|
@ -74,8 +74,7 @@ func (l *localConns) startConnection(orig *packer.Packer) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
p := packer.NewPacker()
|
p := packer.NewPacker(&orig.Header)
|
||||||
p.Header = orig.Header
|
|
||||||
p.Data.AppendBytes(buf[:size])
|
p.Data.AppendBytes(buf[:size])
|
||||||
packed := p.PackV1()
|
packed := p.PackV1()
|
||||||
l.remote.WriteMessage(websocket.BinaryMessage, packed.Bytes())
|
l.remote.WriteMessage(websocket.BinaryMessage, packed.Bytes())
|
||||||
|
|
|
@ -292,11 +292,19 @@ func handleExternalHTTPRequest(ctx context.Context, extConn *WedgeConn, hostname
|
||||||
track := NewTrack(extConn, hostname)
|
track := NewTrack(extConn, hostname)
|
||||||
serverStatus.ExtConnectionRegister(track)
|
serverStatus.ExtConnectionRegister(track)
|
||||||
|
|
||||||
loginfo.Println("Domain Accepted", hostname, extConn.RemoteAddr().String())
|
remoteStr := extConn.RemoteAddr().String()
|
||||||
|
loginfo.Println("Domain Accepted", hostname, remoteStr)
|
||||||
|
|
||||||
rAddr, rPort, err := net.SplitHostPort(extConn.RemoteAddr().String())
|
var header *packer.Header
|
||||||
if err != nil {
|
if rAddr, rPort, err := net.SplitHostPort(remoteStr); err != nil {
|
||||||
loginfo.Println("unable to decode hostport", extConn.RemoteAddr().String())
|
loginfo.Println("unable to decode hostport", remoteStr, err)
|
||||||
|
} else if port, err := strconv.Atoi(rPort); err != nil {
|
||||||
|
loginfo.Printf("unable to parse port string %q: %v\n", rPort, err)
|
||||||
|
} else if header, err = packer.NewHeader(rAddr, port, service); err != nil {
|
||||||
|
loginfo.Println("unable to create packer header", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if header == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,18 +317,8 @@ func handleExternalHTTPRequest(ctx context.Context, extConn *WedgeConn, hostname
|
||||||
|
|
||||||
loginfo.Println("Before Packer", hex.Dump(buffer))
|
loginfo.Println("Before Packer", hex.Dump(buffer))
|
||||||
|
|
||||||
cnt := len(buffer)
|
p := packer.NewPacker(header)
|
||||||
|
p.Data.AppendBytes(buffer)
|
||||||
p := packer.NewPacker()
|
|
||||||
p.Header.SetAddress(rAddr)
|
|
||||||
p.Header.Port, err = strconv.Atoi(rPort)
|
|
||||||
if err != nil {
|
|
||||||
loginfo.Println("Unable to set Remote port", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
p.Header.Service = service
|
|
||||||
p.Data.AppendBytes(buffer[0:cnt])
|
|
||||||
buf := p.PackV1()
|
buf := p.PackV1()
|
||||||
|
|
||||||
//loginfo.Println(hex.Dump(buf.Bytes()))
|
//loginfo.Println(hex.Dump(buf.Bytes()))
|
||||||
|
@ -329,8 +327,8 @@ func handleExternalHTTPRequest(ctx context.Context, extConn *WedgeConn, hostname
|
||||||
sendTrack := NewSendTrack(buf.Bytes(), hostname)
|
sendTrack := NewSendTrack(buf.Bytes(), hostname)
|
||||||
serverStatus.SendExtRequest(conn, sendTrack)
|
serverStatus.SendExtRequest(conn, sendTrack)
|
||||||
|
|
||||||
_, err = extConn.Discard(cnt)
|
cnt := len(buffer)
|
||||||
if err != nil {
|
if _, err = extConn.Discard(cnt); err != nil {
|
||||||
loginfo.Println("unable to discard", cnt, err)
|
loginfo.Println("unable to discard", cnt, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,16 +16,19 @@ const (
|
||||||
|
|
||||||
//Packer -- contains both header and data
|
//Packer -- contains both header and data
|
||||||
type Packer struct {
|
type Packer struct {
|
||||||
Header *packerHeader
|
Header
|
||||||
Data *packerData
|
Data packerData
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewPacker -- Structre
|
// NewPacker creates a new Packer struct using the information from the provided header as
|
||||||
func NewPacker() (p *Packer) {
|
// its own header. (Because the header is stored directly and not as a pointer/reference
|
||||||
p = new(Packer)
|
// it should be safe to override items like the service without affecting the template header.)
|
||||||
p.Header = newPackerHeader()
|
func NewPacker(header *Header) *Packer {
|
||||||
p.Data = newPackerData()
|
p := new(Packer)
|
||||||
return
|
if header != nil {
|
||||||
|
p.Header = *header
|
||||||
|
}
|
||||||
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitHeader(header []byte, names []string) (map[string]string, error) {
|
func splitHeader(header []byte, names []string) (map[string]string, error) {
|
||||||
|
@ -48,7 +51,7 @@ func ReadMessage(b []byte) (*Packer, error) {
|
||||||
// Detect protocol in use
|
// Detect protocol in use
|
||||||
if b[0] == packerV1 {
|
if b[0] == packerV1 {
|
||||||
// Separate the header and body using the header length in the second byte.
|
// Separate the header and body using the header length in the second byte.
|
||||||
p := NewPacker()
|
p := NewPacker(nil)
|
||||||
header := b[2 : b[1]+2]
|
header := b[2 : b[1]+2]
|
||||||
data := b[b[1]+2:]
|
data := b[b[1]+2:]
|
||||||
|
|
||||||
|
@ -69,8 +72,8 @@ func ReadMessage(b []byte) (*Packer, error) {
|
||||||
p.Header.address = net.ParseIP(parts["address"])
|
p.Header.address = net.ParseIP(parts["address"])
|
||||||
if p.Header.address == nil {
|
if p.Header.address == nil {
|
||||||
return nil, fmt.Errorf("Invalid network address %q", parts["address"])
|
return nil, fmt.Errorf("Invalid network address %q", parts["address"])
|
||||||
} else if p.Header.Family() == FamilyIPv4 && p.Header.address.To4() == nil {
|
} else if p.Header.family == FamilyIPv4 && p.Header.address.To4() == nil {
|
||||||
return nil, fmt.Errorf("Address %q is not in address family %s", parts["address"], p.Header.FamilyText())
|
return nil, fmt.Errorf("Address %q is not in address family %s", parts["address"], p.Header.Family())
|
||||||
}
|
}
|
||||||
|
|
||||||
//handle port
|
//handle port
|
||||||
|
@ -79,7 +82,7 @@ func ReadMessage(b []byte) (*Packer, error) {
|
||||||
} else if port <= 0 || port > 65535 {
|
} else if port <= 0 || port > 65535 {
|
||||||
return nil, fmt.Errorf("Port %d out of range", port)
|
return nil, fmt.Errorf("Port %d out of range", port)
|
||||||
} else {
|
} else {
|
||||||
p.Header.Port = port
|
p.Header.port = port
|
||||||
}
|
}
|
||||||
|
|
||||||
//handle data length
|
//handle data length
|
||||||
|
@ -90,7 +93,7 @@ func ReadMessage(b []byte) (*Packer, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//handle Service
|
//handle Service
|
||||||
p.Header.Service = parts["service"]
|
p.Header.service = parts["service"]
|
||||||
|
|
||||||
//handle payload
|
//handle payload
|
||||||
p.Data.AppendBytes(data)
|
p.Data.AppendBytes(data)
|
||||||
|
@ -103,11 +106,11 @@ func ReadMessage(b []byte) (*Packer, error) {
|
||||||
//PackV1 -- Outputs version 1 of packer
|
//PackV1 -- Outputs version 1 of packer
|
||||||
func (p *Packer) PackV1() bytes.Buffer {
|
func (p *Packer) PackV1() bytes.Buffer {
|
||||||
header := strings.Join([]string{
|
header := strings.Join([]string{
|
||||||
p.Header.FamilyText(),
|
p.Header.Family(),
|
||||||
p.Header.AddressString(),
|
p.Header.Address(),
|
||||||
strconv.Itoa(p.Header.Port),
|
strconv.Itoa(p.Header.Port()),
|
||||||
strconv.Itoa(p.Data.DataLen()),
|
strconv.Itoa(p.Data.DataLen()),
|
||||||
p.Header.Service,
|
p.Header.Service(),
|
||||||
}, ",")
|
}, ",")
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
|
@ -9,10 +9,6 @@ type packerData struct {
|
||||||
buffer bytes.Buffer
|
buffer bytes.Buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPackerData() *packerData {
|
|
||||||
return new(packerData)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *packerData) AppendString(dataString string) (int, error) {
|
func (p *packerData) AppendString(dataString string) (int, error) {
|
||||||
return p.buffer.WriteString(dataString)
|
return p.buffer.WriteString(dataString)
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,15 @@ import (
|
||||||
|
|
||||||
type addressFamily int
|
type addressFamily int
|
||||||
|
|
||||||
// packerHeader structure to hold our header information.
|
// The Header struct holds most of the information contained in the header for packets
|
||||||
type packerHeader struct {
|
// between the client and the server (the length of the data is not included here). It
|
||||||
|
// is used to uniquely identify remote connections on the servers end and to communicate
|
||||||
|
// which service the remote client is trying to connect to.
|
||||||
|
type Header struct {
|
||||||
family addressFamily
|
family addressFamily
|
||||||
address net.IP
|
address net.IP
|
||||||
Port int
|
port int
|
||||||
Service string
|
service string
|
||||||
}
|
}
|
||||||
|
|
||||||
//Family -- ENUM for Address Family
|
//Family -- ENUM for Address Family
|
||||||
|
@ -26,16 +29,19 @@ var addressFamilyText = [...]string{
|
||||||
"IPv6",
|
"IPv6",
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPackerHeader() (p *packerHeader) {
|
// NewHeader create a new Header object.
|
||||||
p = new(packerHeader)
|
func NewHeader(address string, port int, service string) (*Header, error) {
|
||||||
p.SetAddress("127.0.0.1")
|
h := new(Header)
|
||||||
p.Port = 65535
|
if err := h.setAddress(address); err != nil {
|
||||||
p.Service = "na"
|
return nil, err
|
||||||
return
|
}
|
||||||
|
h.port = port
|
||||||
|
h.service = service
|
||||||
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//SetAddress -- Set Address. which sets address family automatically
|
// setAddress parses the provided address string and automatically sets the IP family.
|
||||||
func (p *packerHeader) SetAddress(addr string) {
|
func (p *Header) setAddress(addr string) error {
|
||||||
p.address = net.ParseIP(addr)
|
p.address = net.ParseIP(addr)
|
||||||
|
|
||||||
if p.address.To4() != nil {
|
if p.address.To4() != nil {
|
||||||
|
@ -43,30 +49,33 @@ func (p *packerHeader) SetAddress(addr string) {
|
||||||
} else if p.address.To16() != nil {
|
} else if p.address.To16() != nil {
|
||||||
p.family = FamilyIPv6
|
p.family = FamilyIPv6
|
||||||
} else {
|
} else {
|
||||||
panic(fmt.Sprintf("setAddress does not support %q", addr))
|
return fmt.Errorf("invalid IP address %q", addr)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *packerHeader) AddressBytes() []byte {
|
// Family returns the string corresponding to the address's IP family.
|
||||||
if ip4 := p.address.To4(); ip4 != nil {
|
func (p *Header) Family() string {
|
||||||
p.address = ip4
|
return addressFamilyText[p.family]
|
||||||
}
|
|
||||||
|
|
||||||
return []byte(p.address)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *packerHeader) AddressString() string {
|
// Address returns the string form of the header's remote address.
|
||||||
|
func (p *Header) Address() string {
|
||||||
return p.address.String()
|
return p.address.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *packerHeader) Address() net.IP {
|
// Port returns the connected port of the remote connection.
|
||||||
return p.address
|
func (p *Header) Port() int {
|
||||||
|
return p.port
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *packerHeader) Family() addressFamily {
|
// SetService overrides the header's original service. This is primarily useful
|
||||||
return p.family
|
// for sending 'error' and 'end' messages.
|
||||||
|
func (p *Header) SetService(service string) {
|
||||||
|
p.service = service
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *packerHeader) FamilyText() string {
|
// Service returns the service stored in the header.
|
||||||
return addressFamilyText[p.family]
|
func (p *Header) Service() string {
|
||||||
|
return p.service
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue