Package Socket:

- fix tcp,udp,unix socket
- add socket unixgram for session less unix socket
- optmize process & code
- add some standalone test for server / client (add example server config for
  rsyslog)

Package Network/Protocol:
- add unixgram protocol

Package Errors:
- fix bug in loop to prevent circular include
This commit is contained in:
nabbar
2024-01-28 08:20:05 +01:00
parent db0a79e2e7
commit c66a00917a
51 changed files with 2114 additions and 648 deletions

View File

@@ -42,11 +42,7 @@ var _ = Describe("Role", func() {
Context("Creation", func() { Context("Creation", func() {
It("Must fail with invalid json", func() { It("Must fail with invalid json", func() {
/* if minioMode { _, err = cli.Role().Add(name, "{}")
err = fmt.Errorf("backend not compatible following AWS API reference")
} else {
*/_, err = cli.Role().Add(name, "{}")
// }
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
It("Must succeed", func() { It("Must succeed", func() {
@@ -60,11 +56,7 @@ var _ = Describe("Role", func() {
}) })
Context("Attach", func() { Context("Attach", func() {
It("Must fail with invalid params", func() { It("Must fail with invalid params", func() {
/* if minioMode { err = cli.Role().PolicyAttach("policyArn", "roleName")
err = fmt.Errorf("backend not compatible following AWS API reference")
} else {
*/err = cli.Role().PolicyAttach("policyArn", "roleName")
// }
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
It("Must succeed", func() { It("Must succeed", func() {
@@ -85,11 +77,7 @@ var _ = Describe("Role", func() {
}) })
Context("List attached policies to role", func() { Context("List attached policies to role", func() {
It("Must fail with invalid role name", func() { It("Must fail with invalid role name", func() {
/* if minioMode { _, err = cli.Role().PolicyListAttached("invalidRoleName")
err = fmt.Errorf("backend not compatible following AWS API reference")
} else {
*/_, err = cli.Role().PolicyListAttached("invalidRoleName")
// }
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
It("Must return 1 policy", func() { It("Must return 1 policy", func() {
@@ -113,11 +101,7 @@ var _ = Describe("Role", func() {
}) })
Context("Detach", func() { Context("Detach", func() {
It("Must fail with invalid params", func() { It("Must fail with invalid params", func() {
/* if minioMode { err = cli.Role().PolicyDetach("policyArn", "roleName")
err = fmt.Errorf("backend not compatible following AWS API reference")
} else {
*/err = cli.Role().PolicyDetach("policyArn", "roleName")
// }
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
It("Must succeed", func() { It("Must succeed", func() {
@@ -151,12 +135,7 @@ var _ = Describe("Role", func() {
Expect(roleArn).To(Equal(arn)) Expect(roleArn).To(Equal(arn))
}) })
It("Must fail with invalid name", func() { It("Must fail with invalid name", func() {
/* if minioMode { _, err = cli.Role().Check("invalid name")
err = nil
} else {
*/_, err = cli.Role().Check("invalid name")
// }
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
}) })
@@ -189,11 +168,7 @@ var _ = Describe("Role", func() {
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
}) })
It("Must fail", func() { It("Must fail", func() {
/* if minioMode { err = cli.Role().Delete(name)
err = fmt.Errorf("backend not compatible following AWS API reference")
} else {
*/err = cli.Role().Delete(name)
// }
Expect(err).To(HaveOccurred()) Expect(err).To(HaveOccurred())
}) })
}) })

View File

@@ -100,8 +100,10 @@ func (e *ers) Add(parent ...error) {
if er, ok = v.(*ers); ok { if er, ok = v.(*ers); ok {
// prevent circular addition // prevent circular addition
if e.is(er) { if e.IsError(er) {
continue for _, erp := range er.p {
e.Add(erp)
}
} else { } else {
e.p = append(e.p, er) e.p = append(e.p, er)
} }

View File

@@ -46,6 +46,7 @@ const (
NetworkIP NetworkIP
NetworkIP4 NetworkIP4
NetworkIP6 NetworkIP6
NetworkUnixGram
) )
func Parse(str string) NetworkProtocol { func Parse(str string) NetworkProtocol {
@@ -60,6 +61,8 @@ func Parse(str string) NetworkProtocol {
return NetworkUDP return NetworkUDP
case strings.EqualFold(NetworkUnix.Code(), str): case strings.EqualFold(NetworkUnix.Code(), str):
return NetworkUnix return NetworkUnix
case strings.EqualFold(NetworkUnixGram.Code(), str):
return NetworkUnixGram
default: default:
return NetworkEmpty return NetworkEmpty
} }

View File

@@ -56,6 +56,8 @@ func (n NetworkProtocol) String() string {
return "ip4" return "ip4"
case NetworkIP6: case NetworkIP6:
return "ip6" return "ip6"
case NetworkUnixGram:
return "UnixGram"
default: default:
return "" return ""
} }

View File

@@ -35,22 +35,28 @@ import (
"strings" "strings"
libptc "github.com/nabbar/golib/network/protocol" libptc "github.com/nabbar/golib/network/protocol"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
sckclt "github.com/nabbar/golib/socket/client/tcp" sckclt "github.com/nabbar/golib/socket/client/tcp"
sckclu "github.com/nabbar/golib/socket/client/udp" sckclu "github.com/nabbar/golib/socket/client/udp"
sckclx "github.com/nabbar/golib/socket/client/unix" sckclx "github.com/nabbar/golib/socket/client/unix"
sckgrm "github.com/nabbar/golib/socket/client/unixgram"
) )
func New(proto libptc.NetworkProtocol, sizeBufferRead int32, address string) (libsck.Client, error) { func New(proto libptc.NetworkProtocol, sizeBufferRead libsiz.Size, address string) (libsck.Client, error) {
switch proto { switch proto {
case libptc.NetworkUnix: case libptc.NetworkUnix:
if strings.EqualFold(runtime.GOOS, "linux") { if strings.EqualFold(runtime.GOOS, "linux") {
return sckclx.New(sizeBufferRead, address), nil return sckclx.New(sizeBufferRead, address), nil
} }
case libptc.NetworkUnixGram:
if strings.EqualFold(runtime.GOOS, "linux") {
return sckgrm.New(sizeBufferRead, address), nil
}
case libptc.NetworkTCP, libptc.NetworkTCP4, libptc.NetworkTCP6: case libptc.NetworkTCP, libptc.NetworkTCP4, libptc.NetworkTCP6:
return sckclt.New(sizeBufferRead, address) return sckclt.New(sizeBufferRead, address)
case libptc.NetworkUDP, libptc.NetworkUDP4, libptc.NetworkUDP6: case libptc.NetworkUDP, libptc.NetworkUDP4, libptc.NetworkUDP6:
return sckclu.New(address) return sckclu.New(sizeBufferRead, address)
} }
return nil, fmt.Errorf("invalid client protocol") return nil, fmt.Errorf("invalid client protocol")

View File

@@ -33,17 +33,18 @@ import (
"fmt" "fmt"
libptc "github.com/nabbar/golib/network/protocol" libptc "github.com/nabbar/golib/network/protocol"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
sckclt "github.com/nabbar/golib/socket/client/tcp" sckclt "github.com/nabbar/golib/socket/client/tcp"
sckclu "github.com/nabbar/golib/socket/client/udp" sckclu "github.com/nabbar/golib/socket/client/udp"
) )
func New(proto libptc.NetworkProtocol, sizeBufferRead int32, address string) (libsck.Client, error) { func New(proto libptc.NetworkProtocol, sizeBufferRead libsiz.Size, address string) (libsck.Client, error) {
switch proto { switch proto {
case libptc.NetworkTCP, libptc.NetworkTCP4, libptc.NetworkTCP6: case libptc.NetworkTCP, libptc.NetworkTCP4, libptc.NetworkTCP6:
return sckclt.New(sizeBufferRead, address) return sckclt.New(sizeBufferRead, address)
case libptc.NetworkUDP, libptc.NetworkUDP4, libptc.NetworkUDP6: case libptc.NetworkUDP, libptc.NetworkUDP4, libptc.NetworkUDP6:
return sckclu.New(address) return sckclu.New(sizeBufferRead, address)
} }
return nil, fmt.Errorf("invalid client protocol") return nil, fmt.Errorf("invalid client protocol")

View File

@@ -29,10 +29,6 @@ package tcp
import "fmt" import "fmt"
var ( var (
ErrInstance = fmt.Errorf("invalid instance") ErrInstance = fmt.Errorf("invalid instance")
ErrAddress = fmt.Errorf("invalid dial address") ErrAddress = fmt.Errorf("invalid dial address")
ErrHostName = fmt.Errorf("invalid dial host name")
ErrHostPort = fmt.Errorf("invalid dial host port")
ErrContextClosed = fmt.Errorf("context closed")
ErrClientClosed = fmt.Errorf("server closed")
) )

View File

@@ -27,10 +27,13 @@
package tcp package tcp
import ( import (
"net/url" "net"
"strconv"
"sync/atomic" "sync/atomic"
libptc "github.com/nabbar/golib/network/protocol"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
) )
@@ -38,35 +41,25 @@ type ClientTCP interface {
libsck.Client libsck.Client
} }
func New(buffSizeRead int32, address string) (ClientTCP, error) { func New(buffSizeRead libsiz.Size, address string) (ClientTCP, error) {
var ( var (
a = new(atomic.Value) a = new(atomic.Value)
s = new(atomic.Int32) s = new(atomic.Int32)
u = &url.URL{
Host: address,
}
) )
if len(u.Hostname()) < 1 { if len(address) < 1 {
return nil, ErrHostName return nil, ErrAddress
} else if len(u.Port()) < 1 { } else if _, err := net.ResolveTCPAddr(libptc.NetworkTCP.Code(), address); err != nil {
return nil, ErrHostPort return nil, err
} else if i, e := strconv.Atoi(u.Port()); e != nil {
return nil, e
} else if i < 1 || i > 65534 {
return nil, ErrHostPort
} else {
a.Store(u)
} }
s.Store(buffSizeRead) a.Store(address)
s.Store(buffSizeRead.Int32())
return &cltt{ return &cli{
a: a, a: a,
s: s, s: s,
e: new(atomic.Value), e: new(atomic.Value),
i: new(atomic.Value), i: new(atomic.Value),
tr: new(atomic.Value),
tw: new(atomic.Value),
}, nil }, nil
} }

View File

@@ -27,27 +27,25 @@
package tcp package tcp
import ( import (
"bytes" "bufio"
"context" "context"
"errors"
"io" "io"
"net" "net"
"os"
"sync/atomic" "sync/atomic"
libptc "github.com/nabbar/golib/network/protocol" libptc "github.com/nabbar/golib/network/protocol"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
) )
type cltt struct { type cli struct {
a *atomic.Value // address: hostname + port a *atomic.Value // ptr net TCP Addr
s *atomic.Int32 // buffer size s *atomic.Int32 // buffer size
e *atomic.Value // function error e *atomic.Value // function error
i *atomic.Value // function info i *atomic.Value // function info
tr *atomic.Value // connection read timeout
tw *atomic.Value // connection write timeout
} }
func (o *cltt) RegisterFuncError(f libsck.FuncError) { func (o *cli) RegisterFuncError(f libsck.FuncError) {
if o == nil { if o == nil {
return return
} }
@@ -55,7 +53,7 @@ func (o *cltt) RegisterFuncError(f libsck.FuncError) {
o.e.Store(f) o.e.Store(f)
} }
func (o *cltt) RegisterFuncInfo(f libsck.FuncInfo) { func (o *cli) RegisterFuncInfo(f libsck.FuncInfo) {
if o == nil { if o == nil {
return return
} }
@@ -63,7 +61,7 @@ func (o *cltt) RegisterFuncInfo(f libsck.FuncInfo) {
o.i.Store(f) o.i.Store(f)
} }
func (o *cltt) fctError(e error) { func (o *cli) fctError(e error) {
if o == nil { if o == nil {
return return
} }
@@ -74,7 +72,7 @@ func (o *cltt) fctError(e error) {
} }
} }
func (o *cltt) fctInfo(local, remote net.Addr, state libsck.ConnState) { func (o *cli) fctInfo(local, remote net.Addr, state libsck.ConnState) {
if o == nil { if o == nil {
return return
} }
@@ -85,81 +83,115 @@ func (o *cltt) fctInfo(local, remote net.Addr, state libsck.ConnState) {
} }
} }
func (o *cltt) buffRead() *bytes.Buffer { func (o *cli) buffSize() int {
v := o.s.Load() v := o.s.Load()
if v > 0 { if v > 0 {
return bytes.NewBuffer(make([]byte, 0, int(v))) return int(v)
} }
return bytes.NewBuffer(make([]byte, 0, libsck.DefaultBufferSize)) return libsck.DefaultBufferSize
} }
func (o *cltt) dial(ctx context.Context) (net.Conn, error) { func (o *cli) dial(ctx context.Context) (net.Conn, error) {
if o == nil { if o == nil {
return nil, ErrInstance return nil, ErrInstance
} }
v := o.a.Load() v := o.a.Load()
if v == nil { if v == nil {
return nil, ErrAddress return nil, ErrAddress
} else if _, e := os.Stat(v.(string)); e != nil { } else if adr, ok := v.(string); !ok {
return nil, e return nil, ErrAddress
} else { } else {
d := net.Dialer{} d := net.Dialer{}
return d.DialContext(ctx, libptc.NetworkTCP.Code(), v.(string)) return d.DialContext(ctx, libptc.NetworkTCP.Code(), adr)
} }
} }
func (o *cltt) Do(ctx context.Context, request io.Reader, fct libsck.Response) error { func (o *cli) Do(ctx context.Context, request io.Reader, fct libsck.Response) error {
if o == nil { if o == nil {
return ErrInstance return ErrInstance
} }
var ( var (
e error err error
lc net.Addr
rm net.Addr
cnn net.Conn cnn net.Conn
) )
o.fctInfo(nil, nil, libsck.ConnectionDial) defer func() {
if cnn, e = o.dial(ctx); e != nil { if cnn != nil {
o.fctError(e) o.fctInfo(cnn.LocalAddr(), cnn.RemoteAddr(), libsck.ConnectionClose)
return e o.fctError(cnn.Close())
}
defer o.fctError(cnn.Close())
lc = cnn.LocalAddr()
rm = cnn.RemoteAddr()
defer o.fctInfo(lc, rm, libsck.ConnectionClose)
o.fctInfo(lc, rm, libsck.ConnectionNew)
if request != nil {
o.fctInfo(lc, rm, libsck.ConnectionWrite)
if _, e = io.Copy(cnn, request); e != nil {
o.fctError(e)
return e
} }
}()
o.fctInfo(&net.TCPAddr{}, &net.TCPAddr{}, libsck.ConnectionDial)
if cnn, err = o.dial(ctx); err != nil {
o.fctError(err)
return err
} }
o.fctInfo(lc, rm, libsck.ConnectionCloseWrite) o.fctInfo(cnn.LocalAddr(), cnn.RemoteAddr(), libsck.ConnectionNew)
if e = cnn.(*net.TCPConn).CloseWrite(); e != nil {
o.fctError(e)
return e
}
o.fctInfo(lc, rm, libsck.ConnectionHandler) o.sendRequest(cnn, request)
if fct != nil { o.readResponse(cnn, fct)
fct(cnn)
}
o.fctInfo(lc, rm, libsck.ConnectionCloseRead)
if e = cnn.(*net.TCPConn).CloseRead(); e != nil {
o.fctError(e)
return e
}
return nil return nil
} }
func (o *cli) sendRequest(con net.Conn, r io.Reader) {
var (
err error
buf []byte
rdr = bufio.NewReaderSize(r, o.buffSize())
wrt = bufio.NewWriterSize(con, o.buffSize())
)
defer func() {
if con != nil {
if c, ok := con.(*net.TCPConn); ok {
o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionCloseWrite)
_ = c.CloseWrite()
}
}
}()
for {
if con == nil && r == nil {
return
}
buf, err = rdr.ReadBytes('\n')
if err != nil {
if !errors.Is(err, io.EOF) {
o.fctError(err)
}
return
}
o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionWrite)
_, err = wrt.Write(buf)
if err != nil {
o.fctError(err)
return
}
err = wrt.Flush()
if err != nil {
o.fctError(err)
return
}
}
}
func (o *cli) readResponse(con net.Conn, f libsck.Response) {
if f == nil {
return
}
o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionHandler)
f(con)
}

View File

@@ -29,10 +29,6 @@ package udp
import "fmt" import "fmt"
var ( var (
ErrInstance = fmt.Errorf("invalid instance") ErrInstance = fmt.Errorf("invalid instance")
ErrAddress = fmt.Errorf("invalid dial address") ErrAddress = fmt.Errorf("invalid dial address")
ErrHostName = fmt.Errorf("invalid dial host name")
ErrHostPort = fmt.Errorf("invalid dial host port")
ErrContextClosed = fmt.Errorf("context closed")
ErrClientClosed = fmt.Errorf("server closed")
) )

View File

@@ -27,10 +27,13 @@
package udp package udp
import ( import (
"net/url" "net"
"strconv"
"sync/atomic" "sync/atomic"
libsiz "github.com/nabbar/golib/size"
libptc "github.com/nabbar/golib/network/protocol"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
) )
@@ -38,31 +41,25 @@ type ClientUDP interface {
libsck.Client libsck.Client
} }
func New(address string) (ClientUDP, error) { func New(buffSizeRead libsiz.Size, address string) (ClientUDP, error) {
var ( var (
a = new(atomic.Value) a = new(atomic.Value)
u = &url.URL{ s = new(atomic.Int32)
Host: address,
}
) )
if len(u.Hostname()) < 1 { if len(address) < 1 {
return nil, ErrHostName return nil, ErrAddress
} else if len(u.Port()) < 1 { } else if _, err := net.ResolveUDPAddr(libptc.NetworkUDP.Code(), address); err != nil {
return nil, ErrHostPort return nil, err
} else if i, e := strconv.Atoi(u.Port()); e != nil {
return nil, e
} else if i < 1 || i > 65534 {
return nil, ErrHostPort
} else {
a.Store(u)
} }
return &cltu{ a.Store(address)
a: a, s.Store(buffSizeRead.Int32())
e: new(atomic.Value),
i: new(atomic.Value), return &cli{
tr: new(atomic.Value), a: a,
tw: new(atomic.Value), s: s,
e: new(atomic.Value),
i: new(atomic.Value),
}, nil }, nil
} }

View File

@@ -27,26 +27,25 @@
package udp package udp
import ( import (
"bytes" "bufio"
"context" "context"
"errors"
"io" "io"
"net" "net"
"os"
"sync/atomic" "sync/atomic"
libptc "github.com/nabbar/golib/network/protocol" libptc "github.com/nabbar/golib/network/protocol"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
) )
type cltu struct { type cli struct {
a *atomic.Value // address: hostname + port a *atomic.Value // address: hostname + port
e *atomic.Value // function error s *atomic.Int32 // buffer size
i *atomic.Value // function info e *atomic.Value // function error
tr *atomic.Value // connection read timeout i *atomic.Value // function info
tw *atomic.Value // connection write timeout
} }
func (o *cltu) RegisterFuncError(f libsck.FuncError) { func (o *cli) RegisterFuncError(f libsck.FuncError) {
if o == nil { if o == nil {
return return
} }
@@ -54,7 +53,7 @@ func (o *cltu) RegisterFuncError(f libsck.FuncError) {
o.e.Store(f) o.e.Store(f)
} }
func (o *cltu) RegisterFuncInfo(f libsck.FuncInfo) { func (o *cli) RegisterFuncInfo(f libsck.FuncInfo) {
if o == nil { if o == nil {
return return
} }
@@ -62,7 +61,7 @@ func (o *cltu) RegisterFuncInfo(f libsck.FuncInfo) {
o.i.Store(f) o.i.Store(f)
} }
func (o *cltu) fctError(e error) { func (o *cli) fctError(e error) {
if o == nil { if o == nil {
return return
} }
@@ -73,7 +72,7 @@ func (o *cltu) fctError(e error) {
} }
} }
func (o *cltu) fctInfo(local, remote net.Addr, state libsck.ConnState) { func (o *cli) fctInfo(local, remote net.Addr, state libsck.ConnState) {
if o == nil { if o == nil {
return return
} }
@@ -84,64 +83,106 @@ func (o *cltu) fctInfo(local, remote net.Addr, state libsck.ConnState) {
} }
} }
func (o *cltu) buffRead() *bytes.Buffer { func (o *cli) buffSize() int {
return bytes.NewBuffer(make([]byte, 0, 1)) v := o.s.Load()
if v > 0 {
return int(v)
}
return libsck.DefaultBufferSize
} }
func (o *cltu) dial(ctx context.Context) (net.Conn, error) { func (o *cli) dial(ctx context.Context) (net.Conn, error) {
if o == nil { if o == nil {
return nil, ErrInstance return nil, ErrInstance
} }
v := o.a.Load() v := o.a.Load()
if v == nil { if v == nil {
return nil, ErrAddress return nil, ErrAddress
} else if _, e := os.Stat(v.(string)); e != nil { } else if adr, ok := v.(string); !ok {
return nil, e return nil, ErrAddress
} else { } else {
d := net.Dialer{} d := net.Dialer{}
return d.DialContext(ctx, libptc.NetworkUDP.Code(), v.(string)) return d.DialContext(ctx, libptc.NetworkUDP.Code(), adr)
} }
} }
func (o *cltu) Do(ctx context.Context, request io.Reader, fct libsck.Response) error { func (o *cli) Do(ctx context.Context, request io.Reader, fct libsck.Response) error {
if o == nil { if o == nil {
return ErrInstance return ErrInstance
} }
var ( var (
e error err error
lc net.Addr
rm net.Addr
cnn net.Conn cnn net.Conn
) )
o.fctInfo(nil, nil, libsck.ConnectionDial) defer func() {
if cnn, e = o.dial(ctx); e != nil { if cnn != nil {
o.fctError(e) o.fctInfo(cnn.LocalAddr(), cnn.RemoteAddr(), libsck.ConnectionClose)
return e o.fctError(cnn.Close())
}
defer o.fctError(cnn.Close())
lc = cnn.LocalAddr()
rm = cnn.RemoteAddr()
defer o.fctInfo(lc, rm, libsck.ConnectionClose)
o.fctInfo(lc, rm, libsck.ConnectionNew)
if request != nil {
o.fctInfo(lc, rm, libsck.ConnectionWrite)
if _, e = io.Copy(cnn, request); e != nil {
o.fctError(e)
return e
} }
}()
o.fctInfo(&net.UDPAddr{}, &net.UDPAddr{}, libsck.ConnectionDial)
if cnn, err = o.dial(ctx); err != nil {
o.fctError(err)
return err
} }
o.fctInfo(lc, rm, libsck.ConnectionHandler) o.fctInfo(cnn.LocalAddr(), cnn.RemoteAddr(), libsck.ConnectionNew)
if fct != nil {
fct(cnn) o.sendRequest(cnn, request)
} o.readResponse(cnn, fct)
return nil return nil
} }
func (o *cli) sendRequest(con net.Conn, r io.Reader) {
var (
err error
buf []byte
rdr = bufio.NewReaderSize(r, o.buffSize())
wrt = bufio.NewWriterSize(con, o.buffSize())
)
for {
if con == nil && r == nil {
return
}
buf, err = rdr.ReadBytes('\n')
if err != nil {
if !errors.Is(err, io.EOF) {
o.fctError(err)
}
return
}
o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionWrite)
_, err = wrt.Write(buf)
if err != nil {
o.fctError(err)
return
}
err = wrt.Flush()
if err != nil {
o.fctError(err)
return
}
}
}
func (o *cli) readResponse(con net.Conn, f libsck.Response) {
if f == nil {
return
}
o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionHandler)
f(con)
}

View File

@@ -32,8 +32,6 @@ package unix
import "fmt" import "fmt"
var ( var (
ErrInvalidInstance = fmt.Errorf("invalid instance") ErrInstance = fmt.Errorf("invalid instance")
ErrUnixFile = fmt.Errorf("invalid unix file") ErrAddress = fmt.Errorf("invalid dial address")
ErrContextClosed = fmt.Errorf("context closed")
ErrServerClosed = fmt.Errorf("server closed")
) )

View File

@@ -25,3 +25,5 @@
*/ */
package unix package unix
// this file is to prevent error on build with system not compatible with unix

View File

@@ -32,6 +32,8 @@ package unix
import ( import (
"sync/atomic" "sync/atomic"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
) )
@@ -39,21 +41,19 @@ type ClientUnix interface {
libsck.Client libsck.Client
} }
func New(buffSizeRead int32, unixfile string) ClientUnix { func New(buffSizeRead libsiz.Size, unixfile string) ClientUnix {
var ( var (
a = new(atomic.Value) a = new(atomic.Value)
s = new(atomic.Int32) s = new(atomic.Int32)
) )
a.Store(unixfile) a.Store(unixfile)
s.Store(buffSizeRead) s.Store(buffSizeRead.Int32())
return &cltx{ return &cli{
a: a, a: a,
s: s, s: s,
e: new(atomic.Value), e: new(atomic.Value),
i: new(atomic.Value), i: new(atomic.Value),
tr: new(atomic.Value),
tw: new(atomic.Value),
} }
} }

View File

@@ -30,27 +30,25 @@
package unix package unix
import ( import (
"bytes" "bufio"
"context" "context"
"errors"
"io" "io"
"net" "net"
"os"
"sync/atomic" "sync/atomic"
libptc "github.com/nabbar/golib/network/protocol" libptc "github.com/nabbar/golib/network/protocol"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
) )
type cltx struct { type cli struct {
a *atomic.Value // address : unixfile a *atomic.Value // address : unixfile
s *atomic.Int32 // buffer size s *atomic.Int32 // buffer size
e *atomic.Value // function error e *atomic.Value // function error
i *atomic.Value // function info i *atomic.Value // function info
tr *atomic.Value // connection read timeout
tw *atomic.Value // connection write timeout
} }
func (o *cltx) RegisterFuncError(f libsck.FuncError) { func (o *cli) RegisterFuncError(f libsck.FuncError) {
if o == nil { if o == nil {
return return
} }
@@ -58,7 +56,7 @@ func (o *cltx) RegisterFuncError(f libsck.FuncError) {
o.e.Store(f) o.e.Store(f)
} }
func (o *cltx) RegisterFuncInfo(f libsck.FuncInfo) { func (o *cli) RegisterFuncInfo(f libsck.FuncInfo) {
if o == nil { if o == nil {
return return
} }
@@ -66,7 +64,7 @@ func (o *cltx) RegisterFuncInfo(f libsck.FuncInfo) {
o.i.Store(f) o.i.Store(f)
} }
func (o *cltx) fctError(e error) { func (o *cli) fctError(e error) {
if o == nil { if o == nil {
return return
} }
@@ -77,7 +75,7 @@ func (o *cltx) fctError(e error) {
} }
} }
func (o *cltx) fctInfo(local, remote net.Addr, state libsck.ConnState) { func (o *cli) fctInfo(local, remote net.Addr, state libsck.ConnState) {
if o == nil { if o == nil {
return return
} }
@@ -88,84 +86,115 @@ func (o *cltx) fctInfo(local, remote net.Addr, state libsck.ConnState) {
} }
} }
func (o *cltx) buffRead() *bytes.Buffer { func (o *cli) buffSize() int {
v := o.s.Load() v := o.s.Load()
if v > 0 { if v > 0 {
return bytes.NewBuffer(make([]byte, 0, int(v))) return int(v)
} }
return bytes.NewBuffer(make([]byte, 0, libsck.DefaultBufferSize)) return libsck.DefaultBufferSize
} }
func (o *cltx) dial(ctx context.Context) (net.Conn, error) { func (o *cli) dial(ctx context.Context) (net.Conn, error) {
if o == nil { if o == nil {
return nil, ErrInvalidInstance return nil, ErrInstance
} }
v := o.a.Load() v := o.a.Load()
if v == nil { if v == nil {
return nil, ErrUnixFile return nil, ErrAddress
} else if _, e := os.Stat(v.(string)); e != nil { } else if adr, ok := v.(string); !ok {
return nil, e return nil, ErrAddress
} else { } else {
d := net.Dialer{} d := net.Dialer{}
return d.DialContext(ctx, libptc.NetworkUnix.Code(), v.(string)) return d.DialContext(ctx, libptc.NetworkUnix.Code(), adr)
} }
} }
func (o *cltx) Do(ctx context.Context, request io.Reader, fct libsck.Response) error { func (o *cli) Do(ctx context.Context, request io.Reader, fct libsck.Response) error {
if o == nil { if o == nil {
return ErrInvalidInstance return ErrInstance
} }
var ( var (
e error err error
lc net.Addr
rm net.Addr
cnn net.Conn cnn net.Conn
) )
o.fctInfo(nil, nil, libsck.ConnectionDial)
if cnn, e = o.dial(ctx); e != nil {
o.fctError(e)
return e
}
defer func() { defer func() {
err := cnn.Close() if cnn != nil {
o.fctError(err) o.fctInfo(cnn.LocalAddr(), cnn.RemoteAddr(), libsck.ConnectionClose)
o.fctError(cnn.Close())
}
}() }()
lc = cnn.LocalAddr() o.fctInfo(&net.UnixAddr{}, &net.UnixAddr{}, libsck.ConnectionDial)
rm = cnn.RemoteAddr() if cnn, err = o.dial(ctx); err != nil {
o.fctError(err)
defer o.fctInfo(lc, rm, libsck.ConnectionClose) return err
o.fctInfo(lc, rm, libsck.ConnectionNew)
if request != nil {
o.fctInfo(lc, rm, libsck.ConnectionWrite)
if _, e = io.Copy(cnn, request); e != nil {
o.fctError(e)
return e
}
} }
o.fctInfo(lc, rm, libsck.ConnectionCloseWrite) o.fctInfo(cnn.LocalAddr(), cnn.RemoteAddr(), libsck.ConnectionNew)
if e = cnn.(*net.UnixConn).CloseWrite(); e != nil {
o.fctError(e)
return e
}
o.fctInfo(lc, rm, libsck.ConnectionHandler) o.sendRequest(cnn, request)
if fct != nil { o.readResponse(cnn, fct)
fct(cnn)
}
o.fctInfo(lc, rm, libsck.ConnectionCloseRead)
if e = cnn.(*net.UnixConn).CloseRead(); e != nil {
o.fctError(e)
return e
}
return nil return nil
} }
func (o *cli) sendRequest(con net.Conn, r io.Reader) {
var (
err error
buf []byte
rdr = bufio.NewReaderSize(r, o.buffSize())
wrt = bufio.NewWriterSize(con, o.buffSize())
)
defer func() {
if con != nil {
if c, ok := con.(*net.UnixConn); ok {
o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionCloseWrite)
_ = c.CloseWrite()
}
}
}()
for {
if con == nil && r == nil {
return
}
buf, err = rdr.ReadBytes('\n')
if err != nil {
if !errors.Is(err, io.EOF) {
o.fctError(err)
}
return
}
o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionWrite)
_, err = wrt.Write(buf)
if err != nil {
o.fctError(err)
return
}
err = wrt.Flush()
if err != nil {
o.fctError(err)
return
}
}
}
func (o *cli) readResponse(con net.Conn, f libsck.Response) {
if f == nil {
return
}
o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionHandler)
f(con)
}

View File

@@ -0,0 +1,37 @@
//go:build linux
// +build linux
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package unixgram
import "fmt"
var (
ErrInstance = fmt.Errorf("invalid instance")
ErrAddress = fmt.Errorf("invalid dial address")
)

View File

@@ -0,0 +1,29 @@
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package unixgram
// this file is to prevent error on build with system not compatible with unix

View File

@@ -0,0 +1,59 @@
//go:build linux
// +build linux
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package unixgram
import (
"sync/atomic"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket"
)
type ClientUnix interface {
libsck.Client
}
func New(buffSizeRead libsiz.Size, unixfile string) ClientUnix {
var (
a = new(atomic.Value)
s = new(atomic.Int32)
)
a.Store(unixfile)
s.Store(buffSizeRead.Int32())
return &cli{
a: a,
s: s,
e: new(atomic.Value),
i: new(atomic.Value),
}
}

View File

@@ -0,0 +1,191 @@
//go:build linux
// +build linux
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package unixgram
import (
"bufio"
"context"
"errors"
"io"
"net"
"sync/atomic"
libptc "github.com/nabbar/golib/network/protocol"
libsck "github.com/nabbar/golib/socket"
)
type cli struct {
a *atomic.Value // address : unixfile
s *atomic.Int32 // buffer size
e *atomic.Value // function error
i *atomic.Value // function info
}
func (o *cli) RegisterFuncError(f libsck.FuncError) {
if o == nil {
return
}
o.e.Store(f)
}
func (o *cli) RegisterFuncInfo(f libsck.FuncInfo) {
if o == nil {
return
}
o.i.Store(f)
}
func (o *cli) fctError(e error) {
if o == nil {
return
}
v := o.e.Load()
if v != nil {
v.(libsck.FuncError)(e)
}
}
func (o *cli) fctInfo(local, remote net.Addr, state libsck.ConnState) {
if o == nil {
return
}
v := o.i.Load()
if v != nil {
v.(libsck.FuncInfo)(local, remote, state)
}
}
func (o *cli) buffSize() int {
v := o.s.Load()
if v > 0 {
return int(v)
}
return libsck.DefaultBufferSize
}
func (o *cli) dial(ctx context.Context) (net.Conn, error) {
if o == nil {
return nil, ErrInstance
}
v := o.a.Load()
if v == nil {
return nil, ErrAddress
} else if adr, ok := v.(string); !ok {
return nil, ErrAddress
} else {
d := net.Dialer{}
return d.DialContext(ctx, libptc.NetworkUnixGram.Code(), adr)
}
}
func (o *cli) Do(ctx context.Context, request io.Reader, fct libsck.Response) error {
if o == nil {
return ErrInstance
}
var (
err error
cnn net.Conn
)
defer func() {
if cnn != nil {
o.fctInfo(cnn.LocalAddr(), cnn.RemoteAddr(), libsck.ConnectionClose)
o.fctError(cnn.Close())
}
}()
o.fctInfo(&net.UnixAddr{}, &net.UnixAddr{}, libsck.ConnectionDial)
if cnn, err = o.dial(ctx); err != nil {
o.fctError(err)
return err
}
o.fctInfo(cnn.LocalAddr(), cnn.RemoteAddr(), libsck.ConnectionNew)
o.sendRequest(cnn, request)
o.readResponse(cnn, fct)
return nil
}
func (o *cli) sendRequest(con net.Conn, r io.Reader) {
var (
err error
buf []byte
rdr = bufio.NewReaderSize(r, o.buffSize())
wrt = bufio.NewWriterSize(con, o.buffSize())
)
for {
if con == nil && r == nil {
return
}
buf, err = rdr.ReadBytes('\n')
if err != nil {
if !errors.Is(err, io.EOF) {
o.fctError(err)
}
return
}
o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionWrite)
_, err = wrt.Write(buf)
if err != nil {
o.fctError(err)
return
}
err = wrt.Flush()
if err != nil {
o.fctError(err)
return
}
}
}
func (o *cli) readResponse(con net.Conn, f libsck.Response) {
if f == nil {
return
}
o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionHandler)
f(con)
}

View File

@@ -28,6 +28,7 @@ package config
import ( import (
libptc "github.com/nabbar/golib/network/protocol" libptc "github.com/nabbar/golib/network/protocol"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
sckclt "github.com/nabbar/golib/socket/client" sckclt "github.com/nabbar/golib/socket/client"
) )
@@ -35,7 +36,7 @@ import (
type ClientConfig struct { type ClientConfig struct {
Network libptc.NetworkProtocol `` Network libptc.NetworkProtocol ``
Address string Address string
ReadBuffSize int32 ReadBuffSize libsiz.Size
} }
func (o ClientConfig) New() (libsck.Client, error) { func (o ClientConfig) New() (libsck.Client, error) {

View File

@@ -29,7 +29,6 @@ package config
import ( import (
"os" "os"
libdur "github.com/nabbar/golib/duration"
libptc "github.com/nabbar/golib/network/protocol" libptc "github.com/nabbar/golib/network/protocol"
libsiz "github.com/nabbar/golib/size" libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
@@ -40,18 +39,10 @@ type ServerConfig struct {
Network libptc.NetworkProtocol `` Network libptc.NetworkProtocol ``
Address string Address string
PermFile os.FileMode PermFile os.FileMode
GroupPerm int32
BuffSizeRead libsiz.Size BuffSizeRead libsiz.Size
TimeoutRead libdur.Duration
TimeoutWrite libdur.Duration
} }
func (o ServerConfig) New(handler libsck.Handler) (libsck.Server, error) { func (o ServerConfig) New(handler libsck.Handler) (libsck.Server, error) {
s, e := scksrv.New(handler, o.Network, o.BuffSizeRead, o.Address, o.PermFile) return scksrv.New(handler, o.Network, o.BuffSizeRead, o.Address, o.PermFile, o.GroupPerm)
if e != nil {
s.SetReadTimeout(o.TimeoutRead.Time())
s.SetWriteTimeout(o.TimeoutWrite.Time())
}
return s, e
} }

View File

@@ -30,7 +30,6 @@ import (
"context" "context"
"io" "io"
"net" "net"
"time"
libtls "github.com/nabbar/golib/certificates" libtls "github.com/nabbar/golib/certificates"
) )
@@ -84,9 +83,6 @@ type Server interface {
RegisterFuncInfo(f FuncInfo) RegisterFuncInfo(f FuncInfo)
RegisterFuncInfoServer(f FuncInfoSrv) RegisterFuncInfoServer(f FuncInfoSrv)
SetReadTimeout(d time.Duration)
SetWriteTimeout(d time.Duration)
SetTLS(enable bool, config libtls.TLSConfig) error SetTLS(enable bool, config libtls.TLSConfig) error
Listen(ctx context.Context) error Listen(ctx context.Context) error
Shutdown() Shutdown()

View File

@@ -41,15 +41,22 @@ import (
scksrt "github.com/nabbar/golib/socket/server/tcp" scksrt "github.com/nabbar/golib/socket/server/tcp"
scksru "github.com/nabbar/golib/socket/server/udp" scksru "github.com/nabbar/golib/socket/server/udp"
scksrx "github.com/nabbar/golib/socket/server/unix" scksrx "github.com/nabbar/golib/socket/server/unix"
sckgrm "github.com/nabbar/golib/socket/server/unixgram"
) )
func New(handler libsck.Handler, proto libptc.NetworkProtocol, sizeBufferRead libsiz.Size, address string, perm os.FileMode) (libsck.Server, error) { func New(handler libsck.Handler, proto libptc.NetworkProtocol, sizeBufferRead libsiz.Size, address string, perm os.FileMode, gid int32) (libsck.Server, error) {
switch proto { switch proto {
case libptc.NetworkUnix: case libptc.NetworkUnix:
if strings.EqualFold(runtime.GOOS, "linux") { if strings.EqualFold(runtime.GOOS, "linux") {
s := scksrx.New(handler, sizeBufferRead) s := scksrx.New(handler, sizeBufferRead)
s.RegisterSocket(address, perm) e := s.RegisterSocket(address, perm, gid)
return s, nil return s, e
}
case libptc.NetworkUnixGram:
if strings.EqualFold(runtime.GOOS, "linux") {
s := sckgrm.New(handler, sizeBufferRead)
e := s.RegisterSocket(address, perm, gid)
return s, e
} }
case libptc.NetworkTCP, libptc.NetworkTCP4, libptc.NetworkTCP6: case libptc.NetworkTCP, libptc.NetworkTCP4, libptc.NetworkTCP6:
s := scksrt.New(handler, sizeBufferRead) s := scksrt.New(handler, sizeBufferRead)

View File

@@ -40,7 +40,7 @@ import (
scksru "github.com/nabbar/golib/socket/server/udp" scksru "github.com/nabbar/golib/socket/server/udp"
) )
func New(handler libsck.Handler, proto libptc.NetworkProtocol, sizeBufferRead libsiz.Size, address string, perm os.FileMode) (libsck.Server, error) { func New(handler libsck.Handler, proto libptc.NetworkProtocol, sizeBufferRead libsiz.Size, address string, perm os.FileMode, gid int32) (libsck.Server, error) {
switch proto { switch proto {
case libptc.NetworkTCP, libptc.NetworkTCP4, libptc.NetworkTCP6: case libptc.NetworkTCP, libptc.NetworkTCP4, libptc.NetworkTCP6:
s := scksrt.New(handler, sizeBufferRead) s := scksrt.New(handler, sizeBufferRead)

View File

@@ -60,8 +60,6 @@ func New(h libsck.Handler, sizeBuffRead libsiz.Size) ServerTcp {
fe: new(atomic.Value), fe: new(atomic.Value),
fi: new(atomic.Value), fi: new(atomic.Value),
fs: new(atomic.Value), fs: new(atomic.Value),
tr: new(atomic.Value),
tw: new(atomic.Value),
sr: sr, sr: sr,
ad: new(atomic.Value), ad: new(atomic.Value),
} }

View File

@@ -27,52 +27,52 @@
package tcp package tcp
import ( import (
"bufio"
"bytes" "bytes"
"context" "context"
"crypto/tls" "crypto/tls"
"io" "io"
"net" "net"
"net/url"
"time"
libptc "github.com/nabbar/golib/network/protocol" libptc "github.com/nabbar/golib/network/protocol"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
) )
func (o *srv) timeoutRead() time.Time { func (o *srv) buffSize() int {
v := o.tr.Load()
if v != nil {
return time.Now().Add(v.(time.Duration))
}
return time.Time{}
}
func (o *srv) timeoutWrite() time.Time {
v := o.tw.Load()
if v != nil {
return time.Now().Add(v.(time.Duration))
}
return time.Time{}
}
func (o *srv) buffRead() *bytes.Buffer {
v := o.sr.Load() v := o.sr.Load()
if v > 0 { if v > 0 {
return bytes.NewBuffer(make([]byte, 0, int(v))) return int(v)
} }
return bytes.NewBuffer(make([]byte, 0, libsck.DefaultBufferSize)) return libsck.DefaultBufferSize
} }
func (o *srv) getAddress() *url.URL { func (o *srv) getAddress() string {
f := o.ad.Load() f := o.ad.Load()
if f != nil { if f != nil {
return f.(*url.URL) return f.(string)
} }
return nil return ""
}
func (o *srv) getListen(addr string) (net.Listener, error) {
var (
lis net.Listener
err error
)
if lis, err = net.Listen(libptc.NetworkTCP.Code(), addr); err != nil {
return lis, err
} else if t := o.getTLS(); t != nil {
lis = tls.NewListener(lis, t)
o.fctInfoSrv("starting listening socket 'TLS %s %s'", libptc.NetworkTCP.String(), addr)
} else {
o.fctInfoSrv("starting listening socket '%s %s'", libptc.NetworkTCP.String(), addr)
}
return lis, nil
} }
func (o *srv) Listen(ctx context.Context) error { func (o *srv) Listen(ctx context.Context) error {
@@ -82,22 +82,8 @@ func (o *srv) Listen(ctx context.Context) error {
a = o.getAddress() a = o.getAddress()
) )
if a == nil {
return ErrInvalidAddress
} else if t := o.getTLS(); t == nil {
o.fctInfoSrv("starting listening socket '%s %s'", libptc.NetworkTCP.String(), a.Host)
l, e = net.Listen(libptc.NetworkTCP.Code(), a.Host)
} else {
o.fctInfoSrv("starting listening socket 'TLS %s %s'", libptc.NetworkTCP.String(), a.Host)
l, e = tls.Listen(libptc.NetworkTCP.Code(), a.Host, t)
}
if e != nil {
o.fctError(e)
return e
}
var fctClose = func() { var fctClose = func() {
o.fctInfoSrv("closing listen socket '%s %s'", libptc.NetworkTCP.String(), a)
if l != nil { if l != nil {
o.fctError(l.Close()) o.fctError(l.Close())
} }
@@ -105,6 +91,13 @@ func (o *srv) Listen(ctx context.Context) error {
defer fctClose() defer fctClose()
if len(a) == 0 {
return ErrInvalidAddress
} else if l, e = o.getListen(a); e != nil {
o.fctError(e)
return e
}
// Accept new connection or stop if context or shutdown trigger // Accept new connection or stop if context or shutdown trigger
for { for {
select { select {
@@ -116,7 +109,11 @@ func (o *srv) Listen(ctx context.Context) error {
// Accept an incoming connection. // Accept an incoming connection.
if l == nil { if l == nil {
return ErrServerClosed return ErrServerClosed
} else if co, ce := l.Accept(); ce != nil { }
co, ce := l.Accept()
if ce != nil {
o.fctError(ce) o.fctError(ce)
} else { } else {
go o.Conn(co) go o.Conn(co)
@@ -126,52 +123,36 @@ func (o *srv) Listen(ctx context.Context) error {
} }
func (o *srv) Conn(conn net.Conn) { func (o *srv) Conn(conn net.Conn) {
defer o.fctError(conn.Close()) defer func() {
o.fctInfo(conn.LocalAddr(), conn.RemoteAddr(), libsck.ConnectionClose)
_ = conn.Close()
}()
o.fctInfo(conn.LocalAddr(), conn.RemoteAddr(), libsck.ConnectionNew)
var ( var (
lc = conn.LocalAddr() err error
rm = conn.RemoteAddr() rdr = bufio.NewReaderSize(conn, o.buffSize())
tr = o.timeoutRead() buf []byte
tw = o.timeoutWrite() hdl libsck.Handler
br = o.buffRead()
) )
defer o.fctInfo(lc, rm, libsck.ConnectionClose) if hdl = o.handler(); hdl == nil {
o.fctInfo(lc, rm, libsck.ConnectionNew)
if !tr.IsZero() {
if e := conn.SetReadDeadline(tr); e != nil {
o.fctError(e)
return
}
}
if !tw.IsZero() {
if e := conn.SetReadDeadline(tw); e != nil {
o.fctError(e)
return
}
}
o.fctInfo(lc, rm, libsck.ConnectionRead)
if _, e := io.Copy(br, conn); e != nil {
o.fctError(e)
return return
} }
o.fctInfo(lc, rm, libsck.ConnectionCloseRead) for {
if e := conn.(*net.TCPConn).CloseRead(); e != nil { buf, err = rdr.ReadBytes('\n')
o.fctError(e)
return
}
if h := o.handler(); h != nil { o.fctInfo(conn.LocalAddr(), conn.RemoteAddr(), libsck.ConnectionRead)
o.fctInfo(lc, rm, libsck.ConnectionHandler) if err != nil {
h(br, conn) if err != io.EOF {
} o.fctError(err)
}
break
}
o.fctInfo(lc, rm, libsck.ConnectionCloseWrite) o.fctInfo(conn.LocalAddr(), conn.RemoteAddr(), libsck.ConnectionHandler)
if e := conn.(*net.TCPConn).CloseWrite(); e != nil { hdl(bytes.NewBuffer(buf), conn)
o.fctError(e)
} }
} }

View File

@@ -30,10 +30,9 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"net" "net"
"net/url"
"strconv"
"sync/atomic" "sync/atomic"
"time"
libptc "github.com/nabbar/golib/network/protocol"
libtls "github.com/nabbar/golib/certificates" libtls "github.com/nabbar/golib/certificates"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
@@ -64,10 +63,7 @@ type srv struct {
fi *atomic.Value // function info fi *atomic.Value // function info
fs *atomic.Value // function info server fs *atomic.Value // function info server
tr *atomic.Value // connection read timeout
tw *atomic.Value // connection write timeout
sr *atomic.Int32 // read buffer size sr *atomic.Int32 // read buffer size
ad *atomic.Value // Server address url ad *atomic.Value // Server address url
} }
@@ -134,38 +130,14 @@ func (o *srv) RegisterFuncInfoServer(f libsck.FuncInfoSrv) {
o.fs.Store(f) o.fs.Store(f)
} }
func (o *srv) SetReadTimeout(d time.Duration) {
if o == nil {
return
}
o.tr.Store(d)
}
func (o *srv) SetWriteTimeout(d time.Duration) {
if o == nil {
return
}
o.tw.Store(d)
}
func (o *srv) RegisterServer(address string) error { func (o *srv) RegisterServer(address string) error {
var u = &url.URL{ if len(address) < 1 {
Host: address, return ErrInvalidAddress
} else if _, err := net.ResolveTCPAddr(libptc.NetworkTCP.Code(), address); err != nil {
return err
} }
if len(u.Hostname()) < 1 { o.ad.Store(address)
return ErrInvalidHostName
} else if len(u.Port()) < 1 {
return ErrInvalidHostPort
} else if i, e := strconv.Atoi(u.Port()); e != nil {
return e
} else if i < 1 || i > 65534 {
return ErrInvalidHostPort
}
o.ad.Store(u)
return nil return nil
} }

View File

@@ -29,9 +29,8 @@ package udp
import "fmt" import "fmt"
var ( var (
ErrInvalidAddress = fmt.Errorf("invalid listen address") ErrInvalidAddress = fmt.Errorf("invalid listen address")
ErrInvalidHostName = fmt.Errorf("invalid server host name") ErrContextClosed = fmt.Errorf("context closed")
ErrInvalidHostPort = fmt.Errorf("invalid server host port") ErrServerClosed = fmt.Errorf("server closed")
ErrContextClosed = fmt.Errorf("context closed") ErrInvalidHandler = fmt.Errorf("invalid handler")
ErrServerClosed = fmt.Errorf("server closed")
) )

View File

@@ -59,8 +59,6 @@ func New(h libsck.Handler, sizeBuffRead libsiz.Size) ServerTcp {
fe: new(atomic.Value), fe: new(atomic.Value),
fi: new(atomic.Value), fi: new(atomic.Value),
fs: new(atomic.Value), fs: new(atomic.Value),
tr: new(atomic.Value),
tw: new(atomic.Value),
sr: sr, sr: sr,
ad: new(atomic.Value), ad: new(atomic.Value),
} }

View File

@@ -31,71 +31,83 @@ import (
"context" "context"
"io" "io"
"net" "net"
"net/url"
"time"
libptc "github.com/nabbar/golib/network/protocol" libptc "github.com/nabbar/golib/network/protocol"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
) )
func (o *srv) timeoutRead() time.Time { func (o *srv) buffSize() int {
v := o.tr.Load()
if v != nil {
return time.Now().Add(v.(time.Duration))
}
return time.Time{}
}
func (o *srv) timeoutWrite() time.Time {
v := o.tw.Load()
if v != nil {
return time.Now().Add(v.(time.Duration))
}
return time.Time{}
}
func (o *srv) buffRead() *bytes.Buffer {
v := o.sr.Load() v := o.sr.Load()
if v > 0 { if v > 0 {
return bytes.NewBuffer(make([]byte, 0, int(v))) return int(v)
} }
return bytes.NewBuffer(make([]byte, 0, libsck.DefaultBufferSize)) return libsck.DefaultBufferSize
} }
func (o *srv) getAddress() *url.URL { func (o *srv) getAddress() string {
f := o.ad.Load() f := o.ad.Load()
if f != nil { if f != nil {
return f.(*url.URL) return f.(string)
} }
return nil return ""
}
func (o *srv) getListen(addr string) (*net.UDPConn, error) {
var (
adr *net.UDPAddr
lis *net.UDPConn
err error
)
if adr, err = net.ResolveUDPAddr(libptc.NetworkUDP.Code(), addr); err != nil {
return nil, err
} else if lis, err = net.ListenUDP(libptc.NetworkUDP.Code(), adr); err != nil {
return lis, err
} else {
o.fctInfoSrv("starting listening socket '%s %s'", libptc.NetworkUDP.String(), addr)
}
return lis, nil
} }
func (o *srv) Listen(ctx context.Context) error { func (o *srv) Listen(ctx context.Context) error {
var ( var (
e error err error
l net.Listener nbr int
a = o.getAddress() loc *net.UDPAddr
rem net.Addr
con *net.UDPConn
adr = o.getAddress()
hdl libsck.Handler
) )
if a == nil { if len(adr) == 0 {
return ErrInvalidAddress return ErrInvalidAddress
} else if l, e = net.Listen(libptc.NetworkUDP.Code(), a.Host); e != nil { } else if hdl = o.handler(); hdl == nil {
return e return ErrInvalidHandler
} else if loc, err = net.ResolveUDPAddr(libptc.NetworkUDP.Code(), adr); err != nil {
return err
} }
var fctClose = func() { var fctClose = func() {
if l != nil { o.fctInfoSrv("closing listen socket '%s %s'", libptc.NetworkUDP.String(), adr)
o.fctError(l.Close()) if con != nil {
o.fctError(con.Close())
} }
} }
o.fctInfoSrv("starting listening socket 'TLS %s %s'", libptc.NetworkUDP.String(), a.Host)
defer fctClose() defer fctClose()
if con, err = net.ListenUDP(libptc.NetworkUDP.Code(), loc); err != nil {
o.fctError(err)
return err
} else {
o.fctInfoSrv("starting listening socket '%s %s'", libptc.NetworkUDP.String(), adr)
}
// Accept new connection or stop if context or shutdown trigger // Accept new connection or stop if context or shutdown trigger
for { for {
select { select {
@@ -105,53 +117,30 @@ func (o *srv) Listen(ctx context.Context) error {
return nil return nil
default: default:
// Accept an incoming connection. // Accept an incoming connection.
if l == nil { if con == nil {
return ErrServerClosed return ErrServerClosed
} else if co, ce := l.Accept(); ce != nil {
o.fctError(ce)
} else {
go o.Conn(co)
} }
var buf = make([]byte, o.buffSize())
nbr, rem, err = con.ReadFrom(buf)
if rem == nil {
rem = &net.UDPAddr{}
}
o.fctInfo(loc, rem, libsck.ConnectionRead)
if err != nil {
o.fctError(err)
continue
}
go func(la, ra net.Addr, b []byte) {
o.fctInfo(la, ra, libsck.ConnectionHandler)
r := bytes.NewBuffer(b)
r.WriteString("\n")
hdl(r, io.Discard)
}(loc, rem, buf[:nbr])
} }
} }
} }
func (o *srv) Conn(conn net.Conn) {
defer o.fctError(conn.Close())
var (
lc = conn.LocalAddr()
rm = conn.RemoteAddr()
tr = o.timeoutRead()
tw = o.timeoutWrite()
br = o.buffRead()
)
defer o.fctInfo(lc, rm, libsck.ConnectionClose)
o.fctInfo(lc, rm, libsck.ConnectionNew)
if !tr.IsZero() {
if e := conn.SetReadDeadline(tr); e != nil {
o.fctError(e)
return
}
}
if !tw.IsZero() {
if e := conn.SetReadDeadline(tw); e != nil {
o.fctError(e)
return
}
}
o.fctInfo(lc, rm, libsck.ConnectionRead)
if _, e := io.Copy(br, conn); e != nil {
o.fctError(e)
return
}
if h := o.handler(); h != nil {
o.fctInfo(lc, rm, libsck.ConnectionHandler)
h(br, conn)
}
}

View File

@@ -29,10 +29,9 @@ package udp
import ( import (
"fmt" "fmt"
"net" "net"
"net/url"
"strconv"
"sync/atomic" "sync/atomic"
"time"
libptc "github.com/nabbar/golib/network/protocol"
libtls "github.com/nabbar/golib/certificates" libtls "github.com/nabbar/golib/certificates"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
@@ -58,10 +57,7 @@ type srv struct {
fi *atomic.Value // function info fi *atomic.Value // function info
fs *atomic.Value // function info server fs *atomic.Value // function info server
tr *atomic.Value // connection read timeout
tw *atomic.Value // connection write timeout
sr *atomic.Int32 // read buffer size sr *atomic.Int32 // read buffer size
ad *atomic.Value // Server address url ad *atomic.Value // Server address url
} }
@@ -113,38 +109,14 @@ func (o *srv) RegisterFuncInfoServer(f libsck.FuncInfoSrv) {
o.fs.Store(f) o.fs.Store(f)
} }
func (o *srv) SetReadTimeout(d time.Duration) {
if o == nil {
return
}
o.tr.Store(d)
}
func (o *srv) SetWriteTimeout(d time.Duration) {
if o == nil {
return
}
o.tw.Store(d)
}
func (o *srv) RegisterServer(address string) error { func (o *srv) RegisterServer(address string) error {
var u = &url.URL{ if len(address) < 1 {
Host: address, return ErrInvalidAddress
} else if _, err := net.ResolveUDPAddr(libptc.NetworkUDP.Code(), address); err != nil {
return err
} }
if len(u.Hostname()) < 1 { o.ad.Store(address)
return ErrInvalidHostName
} else if len(u.Port()) < 1 {
return ErrInvalidHostPort
} else if i, e := strconv.Atoi(u.Port()); e != nil {
return e
} else if i < 1 || i > 65534 {
return ErrInvalidHostPort
}
o.ad.Store(u)
return nil return nil
} }

View File

@@ -32,6 +32,8 @@ package unix
import "fmt" import "fmt"
var ( var (
ErrContextClosed = fmt.Errorf("context closed") ErrContextClosed = fmt.Errorf("context closed")
ErrServerClosed = fmt.Errorf("server closed") ErrServerClosed = fmt.Errorf("server closed")
ErrInvalidGroup = fmt.Errorf("invalid unix group for socket group permission")
ErrInvalidHandler = fmt.Errorf("invalid handler")
) )

View File

@@ -25,3 +25,5 @@
*/ */
package unix package unix
// this file is to prevent error on build with system not compatible with unix

View File

@@ -37,9 +37,11 @@ import (
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
) )
const maxGID = 32767
type ServerUnix interface { type ServerUnix interface {
libsck.Server libsck.Server
RegisterSocket(unixFile string, perm os.FileMode) RegisterSocket(unixFile string, perm os.FileMode, gid int32) error
} }
func New(h libsck.Handler, sizeBuffRead libsiz.Size) ServerUnix { func New(h libsck.Handler, sizeBuffRead libsiz.Size) ServerUnix {
@@ -64,6 +66,10 @@ func New(h libsck.Handler, sizeBuffRead libsiz.Size) ServerUnix {
sp := new(atomic.Int64) sp := new(atomic.Int64)
sp.Store(0) sp.Store(0)
// socket group permission
sg := new(atomic.Int32)
sg.Store(0)
return &srv{ return &srv{
l: nil, l: nil,
h: f, h: f,
@@ -72,10 +78,9 @@ func New(h libsck.Handler, sizeBuffRead libsiz.Size) ServerUnix {
fe: new(atomic.Value), fe: new(atomic.Value),
fi: new(atomic.Value), fi: new(atomic.Value),
fs: new(atomic.Value), fs: new(atomic.Value),
tr: new(atomic.Value),
tw: new(atomic.Value),
sr: sr, sr: sr,
sf: sf, sf: sf,
sp: sp, sp: sp,
sg: sg,
} }
} }

View File

@@ -30,6 +30,7 @@
package unix package unix
import ( import (
"bufio"
"bytes" "bytes"
"context" "context"
"errors" "errors"
@@ -40,45 +41,24 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"syscall" "syscall"
"time"
libptc "github.com/nabbar/golib/network/protocol" libptc "github.com/nabbar/golib/network/protocol"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
) )
func (o *srv) timeoutRead() time.Time { func (o *srv) buffSize() int {
v := o.tr.Load() v := o.sr.Load()
if v == nil {
return time.Time{} if v > 0 {
} else if d, k := v.(time.Duration); !k { return int(v)
return time.Time{}
} else if d > 0 {
return time.Now().Add(v.(time.Duration))
} }
return time.Time{} return libsck.DefaultBufferSize
}
func (o *srv) timeoutWrite() time.Time {
v := o.tw.Load()
if v == nil {
return time.Time{}
} else if d, k := v.(time.Duration); !k {
return time.Time{}
} else if d > 0 {
return time.Now().Add(v.(time.Duration))
}
return time.Time{}
} }
func (o *srv) buffRead() *bytes.Buffer { func (o *srv) buffRead() *bytes.Buffer {
v := o.sr.Load() return bytes.NewBuffer(make([]byte, o.buffSize()))
if v > 0 {
return bytes.NewBuffer(make([]byte, 0, int(v)))
}
return bytes.NewBuffer(make([]byte, 0, libsck.DefaultBufferSize))
} }
func (o *srv) getSocketFile() (string, error) { func (o *srv) getSocketFile() (string, error) {
@@ -99,6 +79,20 @@ func (o *srv) getSocketPerm() os.FileMode {
return os.FileMode(0770) return os.FileMode(0770)
} }
func (o *srv) getSocketGroup() int {
p := o.sg.Load()
if p >= 0 {
return int(p)
}
gid := syscall.Getgid()
if gid <= maxGID {
return gid
}
return 0
}
func (o *srv) checkFile(unixFile string) (string, error) { func (o *srv) checkFile(unixFile string) (string, error) {
if len(unixFile) < 1 { if len(unixFile) < 1 {
return unixFile, fmt.Errorf("missing socket file path") return unixFile, fmt.Errorf("missing socket file path")
@@ -117,47 +111,73 @@ func (o *srv) checkFile(unixFile string) (string, error) {
return unixFile, nil return unixFile, nil
} }
func (o *srv) getListen(uxf string) (net.Listener, error) {
var (
err error
prm = o.getSocketPerm()
grp = o.getSocketGroup()
old int
inf fs.FileInfo
lis net.Listener
)
old = syscall.Umask(int(prm))
defer func() {
syscall.Umask(old)
}()
if lis, err = net.Listen(libptc.NetworkUnix.Code(), uxf); err != nil {
return nil, err
} else if inf, err = os.Stat(uxf); err != nil {
_ = lis.Close()
return nil, err
} else if inf.Mode() != prm {
if err = os.Chmod(uxf, prm); err != nil {
_ = lis.Close()
return nil, err
}
}
if stt, ok := inf.Sys().(*syscall.Stat_t); ok {
if int(stt.Gid) != grp {
if err = os.Chown(uxf, syscall.Getuid(), grp); err != nil {
_ = lis.Close()
return nil, err
}
}
}
o.fctInfoSrv("starting listening socket '%s %s'", libptc.NetworkUnix.String(), uxf)
return lis, nil
}
func (o *srv) Listen(ctx context.Context) error { func (o *srv) Listen(ctx context.Context) error {
var ( var (
e error e error
i fs.FileInfo f string
l net.Listener l net.Listener
perm = o.getSocketPerm()
unixFile string
p = syscall.Umask(int(perm))
) )
if unixFile, e = o.getSocketFile(); e != nil { if f, e = o.getSocketFile(); e != nil {
return e
} else if l, e = net.Listen(libptc.NetworkUnix.Code(), unixFile); e != nil {
return e
} else if i, e = os.Stat(unixFile); e != nil {
return e return e
} }
syscall.Umask(p)
var fctClose = func() { var fctClose = func() {
if l != nil { if l != nil {
o.fctError(l.Close()) o.fctError(l.Close())
} }
if i, e = os.Stat(unixFile); e == nil { if _, e = os.Stat(f); e == nil {
o.fctError(os.Remove(unixFile)) o.fctError(os.Remove(f))
} }
} }
o.fctInfoSrv("starting listening socket 'TLS %s %s'", libptc.NetworkUnix.String(), unixFile) if l, e = o.getListen(f); e != nil {
return e
}
defer fctClose() defer fctClose()
if i.Mode() != perm {
if e = os.Chmod(unixFile, perm); e != nil {
return e
}
}
// Accept new connection or stop if context or shutdown trigger // Accept new connection or stop if context or shutdown trigger
for { for {
select { select {
@@ -172,63 +192,44 @@ func (o *srv) Listen(ctx context.Context) error {
} else if co, ce := l.Accept(); ce != nil { } else if co, ce := l.Accept(); ce != nil {
o.fctError(ce) o.fctError(ce)
} else { } else {
o.fctInfo(co.LocalAddr(), co.RemoteAddr(), libsck.ConnectionNew)
go o.Conn(co) go o.Conn(co)
} }
} }
} }
} }
func (o *srv) Conn(conn net.Conn) { func (o *srv) Conn(con net.Conn) {
defer func() { defer func() {
e := conn.Close() o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionClose)
o.fctError(e) _ = con.Close()
}() }()
o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionNew)
var ( var (
lc = conn.LocalAddr() err error
rm = conn.RemoteAddr() rdr = bufio.NewReaderSize(con, o.buffSize())
tr = o.timeoutRead() buf []byte
tw = o.timeoutWrite() hdl libsck.Handler
br = o.buffRead()
) )
defer o.fctInfo(lc, rm, libsck.ConnectionClose) if hdl = o.handler(); hdl == nil {
o.fctInfo(lc, rm, libsck.ConnectionNew)
if !tr.IsZero() {
if e := conn.SetReadDeadline(tr); e != nil {
o.fctError(e)
return
}
}
if !tw.IsZero() {
if e := conn.SetReadDeadline(tw); e != nil {
o.fctError(e)
return
}
}
o.fctInfo(lc, rm, libsck.ConnectionRead)
if _, e := io.Copy(br, conn); e != nil {
o.fctError(e)
return return
} }
o.fctInfo(lc, rm, libsck.ConnectionCloseRead) for {
if e := conn.(*net.UnixConn).CloseRead(); e != nil { buf, err = rdr.ReadBytes('\n')
o.fctError(e)
return
}
if h := o.handler(); h != nil { o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionRead)
o.fctInfo(lc, rm, libsck.ConnectionHandler) if err != nil {
h(br, conn) if err != io.EOF {
} o.fctError(err)
}
break
}
o.fctInfo(lc, rm, libsck.ConnectionCloseWrite) o.fctInfo(con.LocalAddr(), con.RemoteAddr(), libsck.ConnectionHandler)
if e := conn.(*net.UnixConn).CloseWrite(); e != nil { hdl(bytes.NewBuffer(buf), con)
o.fctError(e)
} }
} }

View File

@@ -34,7 +34,8 @@ import (
"net" "net"
"os" "os"
"sync/atomic" "sync/atomic"
"time"
libptc "github.com/nabbar/golib/network/protocol"
libtls "github.com/nabbar/golib/certificates" libtls "github.com/nabbar/golib/certificates"
libsck "github.com/nabbar/golib/socket" libsck "github.com/nabbar/golib/socket"
@@ -60,12 +61,10 @@ type srv struct {
fi *atomic.Value // function info fi *atomic.Value // function info
fs *atomic.Value // function info server fs *atomic.Value // function info server
tr *atomic.Value // connection read timeout
tw *atomic.Value // connection write timeout
sr *atomic.Int32 // read buffer size sr *atomic.Int32 // read buffer size
sf *atomic.Value // file unix socket sf *atomic.Value // file unix socket
sp *atomic.Int64 // file unix perm sp *atomic.Int64 // file unix perm
sg *atomic.Int32 // file unix group perm
} }
func (o *srv) Done() <-chan struct{} { func (o *srv) Done() <-chan struct{} {
@@ -116,25 +115,18 @@ func (o *srv) RegisterFuncInfoServer(f libsck.FuncInfoSrv) {
o.fs.Store(f) o.fs.Store(f)
} }
func (o *srv) SetReadTimeout(d time.Duration) { func (o *srv) RegisterSocket(unixFile string, perm os.FileMode, gid int32) error {
if o == nil { if _, err := net.ResolveUnixAddr(libptc.NetworkUnix.Code(), unixFile); err != nil {
return return err
} else if gid > maxGID {
return ErrInvalidGroup
} }
o.tr.Store(d)
}
func (o *srv) SetWriteTimeout(d time.Duration) {
if o == nil {
return
}
o.tw.Store(d)
}
func (o *srv) RegisterSocket(unixFile string, perm os.FileMode) {
o.sf.Store(unixFile) o.sf.Store(unixFile)
o.sp.Store(int64(perm)) o.sp.Store(int64(perm))
o.sg.Store(gid)
return nil
} }
func (o *srv) fctError(e error) { func (o *srv) fctError(e error) {

View File

@@ -0,0 +1,39 @@
//go:build linux
// +build linux
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package unixgram
import "fmt"
var (
ErrContextClosed = fmt.Errorf("context closed")
ErrServerClosed = fmt.Errorf("server closed")
ErrInvalidGroup = fmt.Errorf("invalid unix group for socket group permission")
ErrInvalidHandler = fmt.Errorf("invalid handler")
)

View File

@@ -0,0 +1,29 @@
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package unixgram
// this file is to prevent error on build with system not compatible with unix

View File

@@ -0,0 +1,86 @@
//go:build linux
// +build linux
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package unixgram
import (
"os"
"sync/atomic"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket"
)
const maxGID = 32767
type ServerUnixGram interface {
libsck.Server
RegisterSocket(unixFile string, perm os.FileMode, gid int32) error
}
func New(h libsck.Handler, sizeBuffRead libsiz.Size) ServerUnixGram {
c := new(atomic.Value)
c.Store(make(chan []byte))
s := new(atomic.Value)
s.Store(make(chan struct{}))
f := new(atomic.Value)
f.Store(h)
// socket read buff size
sr := new(atomic.Int32)
sr.Store(sizeBuffRead.Int32())
// socket file
sf := new(atomic.Value)
sf.Store("")
// socket permission
sp := new(atomic.Int64)
sp.Store(0)
// socket group permission
sg := new(atomic.Int32)
sg.Store(0)
return &srv{
l: nil,
h: f,
c: c,
s: s,
fe: new(atomic.Value),
fi: new(atomic.Value),
fs: new(atomic.Value),
sr: sr,
sf: sf,
sp: sp,
sg: sg,
}
}

View File

@@ -0,0 +1,218 @@
//go:build linux
// +build linux
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package unixgram
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/fs"
"net"
"os"
"path/filepath"
"syscall"
libptc "github.com/nabbar/golib/network/protocol"
libsck "github.com/nabbar/golib/socket"
)
func (o *srv) buffSize() int {
v := o.sr.Load()
if v > 0 {
return int(v)
}
return libsck.DefaultBufferSize
}
func (o *srv) getSocketFile() (string, error) {
f := o.sf.Load()
if f != nil {
return o.checkFile(f.(string))
}
return "", os.ErrNotExist
}
func (o *srv) getSocketPerm() os.FileMode {
p := o.sp.Load()
if p > 0 {
return os.FileMode(p)
}
return os.FileMode(0770)
}
func (o *srv) getSocketGroup() int {
p := o.sg.Load()
if p >= 0 {
return int(p)
}
gid := syscall.Getgid()
if gid <= maxGID {
return gid
}
return 0
}
func (o *srv) checkFile(unixFile string) (string, error) {
if len(unixFile) < 1 {
return unixFile, fmt.Errorf("missing socket file path")
} else {
unixFile = filepath.Join(filepath.Dir(unixFile), filepath.Base(unixFile))
}
if _, e := os.Stat(unixFile); e != nil && !errors.Is(e, os.ErrNotExist) {
return unixFile, e
} else if e != nil {
return unixFile, nil
} else if e = os.Remove(unixFile); e != nil {
return unixFile, e
}
return unixFile, nil
}
func (o *srv) getListen(uxf string, adr *net.UnixAddr) (*net.UnixConn, error) {
var (
err error
prm = o.getSocketPerm()
grp = o.getSocketGroup()
old int
inf fs.FileInfo
lis *net.UnixConn
)
old = syscall.Umask(int(prm))
defer func() {
syscall.Umask(old)
}()
if lis, err = net.ListenUnixgram(libptc.NetworkUnixGram.Code(), adr); err != nil {
return nil, err
} else if inf, err = os.Stat(uxf); err != nil {
_ = lis.Close()
return nil, err
} else if inf.Mode() != prm {
if err = os.Chmod(uxf, prm); err != nil {
_ = lis.Close()
return nil, err
}
}
if stt, ok := inf.Sys().(*syscall.Stat_t); ok {
if int(stt.Gid) != grp {
if err = os.Chown(uxf, syscall.Getuid(), grp); err != nil {
_ = lis.Close()
return nil, err
}
}
}
o.fctInfoSrv("starting listening socket '%s %s'", libptc.NetworkUnixGram.String(), uxf)
return lis, nil
}
func (o *srv) Listen(ctx context.Context) error {
var (
err error
nbr int
uxf string
loc *net.UnixAddr
rem net.Addr
con *net.UnixConn
hdl libsck.Handler
)
if uxf, err = o.getSocketFile(); err != nil {
return err
} else if hdl = o.handler(); hdl == nil {
return ErrInvalidHandler
} else if loc, err = net.ResolveUnixAddr(libptc.NetworkUnixGram.Code(), uxf); err != nil {
return err
} else if con, err = o.getListen(uxf, loc); err != nil {
return err
}
var fctClose = func() {
if con != nil {
o.fctInfoSrv("closing listen socket '%s %s'", libptc.NetworkUnixGram.String(), uxf)
o.fctError(con.Close())
}
if _, err = os.Stat(uxf); err == nil {
o.fctError(os.Remove(uxf))
}
}
defer fctClose()
// Accept new connection or stop if context or shutdown trigger
for {
select {
case <-ctx.Done():
return ErrContextClosed
case <-o.Done():
return nil
default:
// Accept an incoming connection.
if con == nil {
return ErrServerClosed
}
var buf = make([]byte, o.buffSize())
nbr, rem, err = con.ReadFrom(buf)
if rem == nil {
rem = &net.UnixAddr{}
}
o.fctInfo(loc, rem, libsck.ConnectionRead)
if err != nil {
o.fctError(err)
continue
}
go func(la, ra net.Addr, b []byte) {
o.fctInfo(la, ra, libsck.ConnectionHandler)
r := bytes.NewBuffer(b)
r.WriteString("\n")
hdl(r, io.Discard)
}(loc, rem, buf[:nbr])
}
}
}

View File

@@ -0,0 +1,176 @@
//go:build linux
// +build linux
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package unixgram
import (
"fmt"
"net"
"os"
"sync/atomic"
libptc "github.com/nabbar/golib/network/protocol"
libtls "github.com/nabbar/golib/certificates"
libsck "github.com/nabbar/golib/socket"
)
var (
closedChanStruct chan struct{}
)
func init() {
closedChanStruct = make(chan struct{})
close(closedChanStruct)
}
type srv struct {
l net.Listener
h *atomic.Value // handler
c *atomic.Value // chan []byte
s *atomic.Value // chan struct{}
fe *atomic.Value // function error
fi *atomic.Value // function info
fs *atomic.Value // function info server
sr *atomic.Int32 // read buffer size
sf *atomic.Value // file unix socket
sp *atomic.Int64 // file unix perm
sg *atomic.Int32 // file unix group perm
}
func (o *srv) Done() <-chan struct{} {
s := o.s.Load()
if s != nil {
return s.(chan struct{})
}
return closedChanStruct
}
func (o *srv) Shutdown() {
if o == nil {
return
}
s := o.s.Load()
if s != nil {
o.s.Store(nil)
}
}
func (o *srv) SetTLS(enable bool, config libtls.TLSConfig) error {
return nil
}
func (o *srv) RegisterFuncError(f libsck.FuncError) {
if o == nil {
return
}
o.fe.Store(f)
}
func (o *srv) RegisterFuncInfo(f libsck.FuncInfo) {
if o == nil {
return
}
o.fi.Store(f)
}
func (o *srv) RegisterFuncInfoServer(f libsck.FuncInfoSrv) {
if o == nil {
return
}
o.fs.Store(f)
}
func (o *srv) RegisterSocket(unixFile string, perm os.FileMode, gid int32) error {
if _, err := net.ResolveUnixAddr(libptc.NetworkUnixGram.Code(), unixFile); err != nil {
return err
} else if gid > maxGID {
return ErrInvalidGroup
}
o.sf.Store(unixFile)
o.sp.Store(int64(perm))
o.sg.Store(gid)
return nil
}
func (o *srv) fctError(e error) {
if o == nil {
return
}
v := o.fe.Load()
if v != nil {
v.(libsck.FuncError)(e)
}
}
func (o *srv) fctInfo(local, remote net.Addr, state libsck.ConnState) {
if o == nil {
return
}
v := o.fi.Load()
if v != nil {
v.(libsck.FuncInfo)(local, remote, state)
}
}
func (o *srv) fctInfoSrv(msg string, args ...interface{}) {
if o == nil {
return
}
v := o.fs.Load()
if v != nil {
v.(libsck.FuncInfoSrv)(fmt.Sprintf(msg, args...))
}
}
func (o *srv) handler() libsck.Handler {
if o == nil {
return nil
}
v := o.h.Load()
if v != nil {
return v.(libsck.Handler)
}
return nil
}

View File

@@ -0,0 +1,102 @@
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package main
import (
"bytes"
"context"
"fmt"
"io"
"net"
"os"
"time"
netptl "github.com/nabbar/golib/network/protocol"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket"
sckcfg "github.com/nabbar/golib/socket/config"
)
func config() sckcfg.ClientConfig {
return sckcfg.ClientConfig{
Network: netptl.NetworkTCP,
Address: ":9000",
ReadBuffSize: 32 * libsiz.SizeKilo,
}
}
func printError(err ...error) {
for _, e := range err {
if e == nil {
continue
}
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", e)
}
}
func checkPanic(err ...error) {
var found = false
for _, e := range err {
if e == nil {
continue
}
found = true
printError(err...)
break
}
if found {
panic(nil)
}
}
func request() io.Reader {
var buf = bytes.NewBuffer(make([]byte, 0))
buf.WriteString(fmt.Sprintf("<14>%s myapp: This is a sample syslog message #%d\n", time.Now().Format(time.RFC3339), buf.Len()))
buf.WriteString(fmt.Sprintf("<14>%s myapp: This is a sample syslog message #%d\n", time.Now().Format(time.RFC3339), buf.Len()))
buf.WriteString(fmt.Sprintf("<14>%s myapp: This is a sample syslog message #%d\n", time.Now().Format(time.RFC3339), buf.Len()))
buf.WriteString(fmt.Sprintf("<14>%s myapp: This is a sample syslog message #%d\n", time.Now().Format(time.RFC3339), buf.Len()))
return buf
}
func main() {
cli, err := config().New()
checkPanic(err)
cli.RegisterFuncError(func(e error) {
printError(e)
})
cli.RegisterFuncInfo(func(local, remote net.Addr, state libsck.ConnState) {
_, _ = fmt.Fprintf(os.Stdout, "[%s %s]=>[%s %s] %s\n", remote.Network(), remote.String(), local.Network(), local.String(), state.String())
})
checkPanic(cli.Do(context.Background(), request(), func(r io.Reader) {
_, e := io.Copy(os.Stdout, r)
printError(e)
}))
}

View File

@@ -0,0 +1,98 @@
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package main
import (
"bytes"
"context"
"fmt"
"io"
"net"
"os"
"time"
netptl "github.com/nabbar/golib/network/protocol"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket"
sckcfg "github.com/nabbar/golib/socket/config"
)
func config() sckcfg.ClientConfig {
return sckcfg.ClientConfig{
Network: netptl.NetworkUDP,
Address: ":9001",
ReadBuffSize: 32 * libsiz.SizeKilo,
}
}
func printError(err ...error) {
for _, e := range err {
if e == nil {
continue
}
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", e)
}
}
func checkPanic(err ...error) {
var found = false
for _, e := range err {
if e == nil {
continue
}
found = true
printError(err...)
break
}
if found {
panic(nil)
}
}
func request() io.Reader {
var buf = bytes.NewBuffer(make([]byte, 0))
for i := 1; i <= 100; i++ {
buf.WriteString(fmt.Sprintf("<14>%s myapp: This is a sample syslog message #%d buff: %d\n", time.Now().Format(time.RFC3339), i, buf.Len()))
}
return buf
}
func main() {
cli, err := config().New()
checkPanic(err)
cli.RegisterFuncError(func(e error) {
printError(e)
})
cli.RegisterFuncInfo(func(local, remote net.Addr, state libsck.ConnState) {
_, _ = fmt.Fprintf(os.Stdout, "[%s %s]=>[%s %s] %s\n", remote.Network(), remote.String(), local.Network(), local.String(), state.String())
})
checkPanic(cli.Do(context.Background(), request(), nil))
}

View File

@@ -0,0 +1,99 @@
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package main
import (
"bytes"
"context"
"fmt"
"io"
"net"
"os"
"time"
netptl "github.com/nabbar/golib/network/protocol"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket"
sckcfg "github.com/nabbar/golib/socket/config"
)
func config() sckcfg.ClientConfig {
return sckcfg.ClientConfig{
Network: netptl.NetworkUnixGram,
Address: "/tmp/test-server-unix.sock",
ReadBuffSize: 32 * libsiz.SizeKilo,
}
}
func printError(err ...error) {
for _, e := range err {
if e == nil {
continue
}
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", e)
}
}
func checkPanic(err ...error) {
var found = false
for _, e := range err {
if e == nil {
continue
}
found = true
printError(err...)
break
}
if found {
panic(nil)
}
}
func request() io.Reader {
var buf = bytes.NewBuffer(make([]byte, 0))
buf.WriteString(fmt.Sprintf("<14>%s myapp: This is a sample syslog message #%d\n", time.Now().Format(time.RFC3339), buf.Len()))
buf.WriteString(fmt.Sprintf("<14>%s myapp: This is a sample syslog message #%d\n", time.Now().Format(time.RFC3339), buf.Len()))
buf.WriteString(fmt.Sprintf("<14>%s myapp: This is a sample syslog message #%d\n", time.Now().Format(time.RFC3339), buf.Len()))
buf.WriteString(fmt.Sprintf("<14>%s myapp: This is a sample syslog message #%d\n", time.Now().Format(time.RFC3339), buf.Len()))
return buf
}
func main() {
cli, err := config().New()
checkPanic(err)
cli.RegisterFuncError(func(e error) {
printError(e)
})
cli.RegisterFuncInfo(func(local, remote net.Addr, state libsck.ConnState) {
_, _ = fmt.Fprintf(os.Stdout, "[%s %s]=>[%s %s] %s\n", remote.Network(), remote.String(), local.Network(), local.String(), state.String())
})
checkPanic(cli.Do(context.Background(), request(), nil))
}

View File

@@ -0,0 +1,9 @@
## TCP Forward
if $syslogtag contains 'testme' then {
Action(
Type="omfwd"
Target="127.0.0.1"
Port="9000"
Protocol="tcp"
)
}

View File

@@ -0,0 +1,97 @@
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package main
import (
"context"
"fmt"
"io"
"net"
"os"
netptl "github.com/nabbar/golib/network/protocol"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket"
sckcfg "github.com/nabbar/golib/socket/config"
)
func config() sckcfg.ServerConfig {
return sckcfg.ServerConfig{
Network: netptl.NetworkTCP,
Address: ":9000",
PermFile: 0,
BuffSizeRead: 32 * libsiz.SizeKilo,
}
}
func Handler(request io.Reader, response io.Writer) {
_, e := io.Copy(os.Stdout, request)
printError(e)
}
func printError(err ...error) {
for _, e := range err {
if e == nil {
continue
}
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", e)
}
}
func checkPanic(err ...error) {
var found = false
for _, e := range err {
if e == nil {
continue
}
found = true
printError(err...)
break
}
if found {
panic(nil)
}
}
func main() {
srv, err := config().New(Handler)
checkPanic(err)
srv.RegisterFuncError(func(e error) {
printError(e)
})
srv.RegisterFuncInfo(func(local, remote net.Addr, state libsck.ConnState) {
_, _ = fmt.Fprintf(os.Stdout, "[%s %s]=>[%s %s] %s\n", remote.Network(), remote.String(), local.Network(), local.String(), state.String())
})
srv.RegisterFuncInfoServer(func(msg string) {
_, _ = fmt.Fprintf(os.Stdout, "%s\n", msg)
})
checkPanic(srv.Listen(context.Background()))
}

View File

@@ -0,0 +1,10 @@
## UDP Forward
if $syslogtag contains 'testme' then {
Action(
Type="omfwd"
Target="127.0.0.1"
Port="9001"
Protocol="udp"
udp.sendToAll="on"
)
}

View File

@@ -0,0 +1,97 @@
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package main
import (
"context"
"fmt"
"io"
"net"
"os"
netptl "github.com/nabbar/golib/network/protocol"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket"
sckcfg "github.com/nabbar/golib/socket/config"
)
func config() sckcfg.ServerConfig {
return sckcfg.ServerConfig{
Network: netptl.NetworkUDP,
Address: ":9001",
PermFile: 0,
BuffSizeRead: 32 * libsiz.SizeKilo,
}
}
func Handler(request io.Reader, response io.Writer) {
_, e := io.Copy(os.Stdout, request)
printError(e)
}
func printError(err ...error) {
for _, e := range err {
if e == nil {
continue
}
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", e)
}
}
func checkPanic(err ...error) {
var found = false
for _, e := range err {
if e == nil {
continue
}
found = true
printError(err...)
break
}
if found {
panic(nil)
}
}
func main() {
srv, err := config().New(Handler)
checkPanic(err)
srv.RegisterFuncError(func(e error) {
printError(e)
})
srv.RegisterFuncInfo(func(local, remote net.Addr, state libsck.ConnState) {
_, _ = fmt.Fprintf(os.Stdout, "[%s %s]=>[%s %s] %s\n", remote.Network(), remote.String(), local.Network(), local.String(), state.String())
})
srv.RegisterFuncInfoServer(func(msg string) {
_, _ = fmt.Fprintf(os.Stdout, "%s\n", msg)
})
checkPanic(srv.Listen(context.Background()))
}

View File

@@ -0,0 +1,13 @@
## Unix Forward using sock session less
## So unixgram type and as unix socket
## (based on UDP socket)
if $syslogtag contains 'testme' then {
Action(
Type="omfwd"
Target="127.0.0.1"
Port="9001"
Protocol="udp"
udp.sendToAll="on"
)
}

View File

@@ -0,0 +1,98 @@
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package main
import (
"context"
"fmt"
"io"
"net"
"os"
netptl "github.com/nabbar/golib/network/protocol"
libsiz "github.com/nabbar/golib/size"
libsck "github.com/nabbar/golib/socket"
sckcfg "github.com/nabbar/golib/socket/config"
)
func config() sckcfg.ServerConfig {
return sckcfg.ServerConfig{
Network: netptl.NetworkUnixGram,
Address: "/tmp/test-server-unix.sock",
PermFile: 0777,
GroupPerm: -1,
BuffSizeRead: 32 * libsiz.SizeKilo,
}
}
func Handler(request io.Reader, response io.Writer) {
_, e := io.Copy(os.Stdout, request)
printError(e)
}
func printError(err ...error) {
for _, e := range err {
if e == nil {
continue
}
_, _ = fmt.Fprintf(os.Stderr, "Error: %v\n", e)
}
}
func checkPanic(err ...error) {
var found = false
for _, e := range err {
if e == nil {
continue
}
found = true
printError(err...)
break
}
if found {
panic(nil)
}
}
func main() {
srv, err := config().New(Handler)
checkPanic(err)
srv.RegisterFuncError(func(e error) {
printError(e)
})
srv.RegisterFuncInfo(func(local, remote net.Addr, state libsck.ConnState) {
_, _ = fmt.Fprintf(os.Stdout, "[%s %s]=>[%s %s] %s\n", remote.Network(), remote.String(), local.Network(), local.String(), state.String())
})
srv.RegisterFuncInfoServer(func(msg string) {
_, _ = fmt.Fprintf(os.Stdout, "%s\n", msg)
})
checkPanic(srv.Listen(context.Background()))
}