Skip to content

Instantly share code, notes, and snippets.

@WinXaito

WinXaito/main.go Secret

Created December 21, 2021 16:47
Show Gist options
  • Save WinXaito/c4eba0e808c8587dbdbd2a2687260bd5 to your computer and use it in GitHub Desktop.
Save WinXaito/c4eba0e808c8587dbdbd2a2687260bd5 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"log"
"runtime"
"sync"
"syscall"
"time"
"unsafe"
)
var (
fw = syscall.NewLazyDLL("Fwlib64.dll")
cmdAllclibhndl3 = fw.NewProc("cnc_allclibhndl3") // Allocate library handle 3
cmdFreelibhndl = fw.NewProc("cnc_freelibhndl") // Free library handle
cmdGetpath = fw.NewProc("cnc_getpath") // Get the channel of the CNC
)
type Focas struct {
cancel context.CancelFunc
wg *sync.WaitGroup
ctx context.Context
mainHandle *uint16
}
func init() {
if err := fw.Load(); err != nil {
log.Fatalln(err)
}
}
func main() {
println("MAIN")
f := NewFocas()
f.open()
defer f.close()
measure("Loop 100", func() error {
for i := 0; i < 100; i++ {
f.callMethod()
}
return nil
})
time.Sleep(time.Second)
}
func NewFocas() *Focas {
ctx, cancel := context.WithCancel(context.Background())
return &Focas{
cancel: cancel,
wg: &sync.WaitGroup{},
ctx: ctx,
mainHandle: nil,
}
}
func (f *Focas) callMethod() {
// On verouille le thread actif et on le déverouille à la fin de l'exécution
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// On demande on nouvel handle
handle, err := acquireHandle()
if err != nil {
log.Fatalln(err)
}
// Exécution de la méthode voulu
var pathNo *int16
var maxpathNo *int16
pathNo = new(int16)
maxpathNo = new(int16)
ret, _, err := cmdGetpath.Call(uintptr(*handle),
uintptr(unsafe.Pointer(pathNo)),
uintptr(unsafe.Pointer(maxpathNo)))
if err != nil && ret != 0 {
log.Fatalln(err)
}
// On referme l'handle
err = closeHandle(handle)
if err != nil {
log.Fatalln(err)
}
}
func (f *Focas) open() {
f.wg.Add(1)
go func() {
defer f.wg.Done()
// Acquire the main handle
handle, err := acquireHandle()
if err != nil {
log.Fatalln(err)
}
println("Main Handle:", *handle)
f.mainHandle = handle
// On attend la demande d'arrêt
<-f.ctx.Done()
// Release the main handle
err = closeHandle(f.mainHandle)
if err != nil {
log.Fatalln(err)
}
}()
}
func (f *Focas) close() {
f.cancel()
f.wg.Wait()
}
func acquireHandle() (*uint16, error) {
var handle *uint16
handle = new(uint16)
ipPtr, _ := syscall.BytePtrFromString("192.168.100.229")
ret, _, err := cmdAllclibhndl3.Call(uintptr(unsafe.Pointer(ipPtr)),
uintptr(8193),
uintptr(5),
uintptr(unsafe.Pointer(handle)))
if err != nil && ret != 0 {
return nil, err
}
return handle, nil
}
func closeHandle(handle *uint16) error {
ret, _, err := cmdFreelibhndl.Call(uintptr(*handle))
if err != nil && ret != 0 {
return err
}
return nil
}
func measure(d string, m func() error) {
s := time.Now()
err := m()
if err != nil {
log.Println(err)
}
f := time.Since(s)
fmt.Printf("%s: Execution time: %s (%dns)\n", d, f, f.Nanoseconds())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment