Skip to content

Instantly share code, notes, and snippets.

@lesismal
Last active April 23, 2023 04:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lesismal/316b711a7f39cc539cebaad6c8e5b0fd to your computer and use it in GitHub Desktop.
Save lesismal/316b711a7f39cc539cebaad6c8e5b0fd to your computer and use it in GitHub Desktop.
listener_wraper.go
package main
import (
"context"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"sync"
"time"
"github.com/lesismal/nbio/nbhttp"
)
var connections = sync.Map{}
func nbioOnEcho(w http.ResponseWriter, r *http.Request) {
res, ok := w.(*nbhttp.Response)
if ok {
conn := res.Parser.Processor.Conn()
yourData, ok := connections.Load(conn)
if ok {
fmt.Println("nbioServer onEcho:", yourData)
} else {
fmt.Println("nbioServer onEcho: not found connection")
}
}
w.Write([]byte(time.Now().Format("20060102 15:04:05")))
}
func nbioServer() {
mux := &http.ServeMux{}
mux.HandleFunc("/", nbioOnEcho)
engine := nbhttp.NewServer(nbhttp.Config{
Network: "tcp",
Addrs: []string{"localhost:8080"},
Handler: mux,
IOMod: nbhttp.IOModBlocking,
})
engine.OnOpen(func(conn net.Conn) {
fmt.Println("nbioServer onOpen:", conn.RemoteAddr())
yourData := "data: " + conn.RemoteAddr().String() // or other things
connections.Store(conn, yourData)
})
engine.OnClose(func(conn net.Conn, err error) {
connections.Delete(conn)
fmt.Println("nbioServer onClose:", conn.RemoteAddr(), err)
})
err := engine.Start()
if err != nil {
fmt.Printf("nbio.Start failed: %v\n", err)
return
}
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
<-interrupt
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
engine.Shutdown(ctx)
}
func netOnEcho(w http.ResponseWriter, r *http.Request) {
yourData, ok := connections.Load(r.RemoteAddr)
if ok {
fmt.Println("netServer onEcho:", yourData)
} else {
fmt.Println("netServer onEcho: not found connection " + r.RemoteAddr)
}
w.Write([]byte(time.Now().Format("20060102 15:04:05")))
}
func netServer() {
mux := &http.ServeMux{}
mux.HandleFunc("/", netOnEcho)
server := &http.Server{
Addr: "localhost:8080",
Handler: mux,
ConnState: func(conn net.Conn, state http.ConnState) {
switch state {
case http.StateNew:
fmt.Println("netServer onOpen:", conn.RemoteAddr())
remoteAddr := conn.RemoteAddr().String()
yourData := "data: " + remoteAddr // or other things
connections.Store(remoteAddr, yourData)
case http.StateActive:
case http.StateIdle:
case http.StateHijacked:
case http.StateClosed:
connections.Delete(conn)
fmt.Println("netServer onClose:", conn.RemoteAddr())
}
},
}
err := server.ListenAndServe()
fmt.Println("server exit:", err)
}
func main() {
netServer()
// nbioServer()
}
@lesismal
Copy link
Author

lesismal commented Apr 23, 2023

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"
	"os/signal"
	"time"

	"github.com/jbremer/ja3"
	"github.com/lesismal/llib/std/crypto/tls"
	"github.com/lesismal/nbio/nbhttp"
)

func onEcho(w http.ResponseWriter, r *http.Request) {
	res, ok := w.(*nbhttp.Response)
	if ok {
		tlsConn, ok := res.Parser.Processor.Conn().(*tls.Conn)
		if ok {
			clientHello := tlsConn.ClientHello()
			data := clientHello.Marshal()
			j, err := ja3.ComputeJA3FromHandshake(data)
			if err != nil {
				panic(err)
			}
			fmt.Println("JA3 SNI:", j.GetSNI())
			fmt.Println("JA3 Hash:", j.GetJA3Hash())
			fmt.Println("JA3 String:", j.GetJA3String())
		} else {
			fmt.Println("nbioServer onEcho: not found connection")
		}
	}
	w.Write([]byte(time.Now().Format("20060102 15:04:05")))
}

func main() {
	mux := &http.ServeMux{}
	mux.HandleFunc("/", onEcho)

	cert, err := tls.LoadX509KeyPair("server.crt", "server.key")
	if err != nil {
		log.Fatalf("tls.X509KeyPair failed: %v", err)
	}
	tlsConfig := &tls.Config{
		Certificates:       []tls.Certificate{cert},
		InsecureSkipVerify: true,
	}

	engine := nbhttp.NewServer(nbhttp.Config{
		Network:   "tcp",
		AddrsTLS:  []string{"localhost:8080"},
		Handler:   mux,
		IOMod:     nbhttp.IOModBlocking,
		TLSConfig: tlsConfig,
	})

	err = engine.Start()
	if err != nil {
		fmt.Printf("nbio.Start failed: %v\n", err)
		return
	}

	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, os.Interrupt)
	<-interrupt

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
	defer cancel()
	engine.Shutdown(ctx)
}

output:

2023/04/23 12:53:18.100 [INF] NBIO[NB] start
2023/04/23 12:53:18.133 [INF] Serve     TLS On: [tcp@127.0.0.1:8080]
JA3 SNI: localhost
JA3 Hash: 978a247d4bb07b29fe4c6693b6e7ffeb
JA3 String: 771,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,18-27-10-65281-17513-0-43-23-16-13-11-5-35-51-45-21,29-23-24,0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment