From 82e3708edceb8e4b5afa06eab374572f5e03b2a0 Mon Sep 17 00:00:00 2001 From: lysShub Date: Sun, 26 May 2024 06:28:01 +0800 Subject: [PATCH] gofmt --- dll/dll_test.go | 42 +++++------ dll/mem.go | 182 ++++++++++++++++++++++++------------------------ dll/sys.go | 30 ++++---- 3 files changed, 127 insertions(+), 127 deletions(-) diff --git a/dll/dll_test.go b/dll/dll_test.go index 0688fdb..a0f08a9 100644 --- a/dll/dll_test.go +++ b/dll/dll_test.go @@ -1,21 +1,21 @@ -//go:build windows -// +build windows - -package dll_test - -import ( - "testing" - - "github.com/lysShub/divert-go/dll" -) - -var ( - test = dll.NewLazyDLL(make([]byte, 3)) - openProc = test.NewProc("WinDivertOpen") -) - -func TestXxx(t *testing.T) { - dll.ResetLazyDll(test, `D:\OneDrive\code\go\divert-go\embed\WinDivert64.dll`) - - openProc.Find() -} +//go:build windows +// +build windows + +package dll_test + +import ( + "testing" + + "github.com/lysShub/divert-go/dll" +) + +var ( + test = dll.NewLazyDLL(make([]byte, 3)) + openProc = test.NewProc("WinDivertOpen") +) + +func TestXxx(t *testing.T) { + dll.ResetLazyDll(test, `D:\OneDrive\code\go\divert-go\embed\WinDivert64.dll`) + + openProc.Find() +} diff --git a/dll/mem.go b/dll/mem.go index 9940e72..201b20b 100644 --- a/dll/mem.go +++ b/dll/mem.go @@ -1,91 +1,91 @@ -package dll - -import ( - "sync" - "sync/atomic" - "syscall" - "unsafe" - - "github.com/pkg/errors" - "golang.zx2c4.com/wireguard/windows/driver/memmod" -) - -type MemLazyDll struct { - Data []byte - - mu sync.Mutex - dll *memmod.Module -} - -var _ LazyDll = (*MemLazyDll)(nil) - -func (d *MemLazyDll) Handle() uintptr { - d.mustLoad() - return d.dll.BaseAddr() -} -func (d *MemLazyDll) Load() (err error) { - if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll))) != nil { - return nil - } - d.mu.Lock() - defer d.mu.Unlock() - if d.dll != nil { - return nil - } - - dll, err := memmod.LoadLibrary(d.Data) - if err != nil { - return errors.WithStack(err) - } - atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll)), unsafe.Pointer(dll)) - return nil -} -func (d *MemLazyDll) mustLoad() { - err := d.Load() - if err != nil { - panic(err) - } -} - -func (d *MemLazyDll) NewProc(name string) LazyProc { - return &MemLazyProc{Name: name, l: d} -} - -type MemLazyProc struct { - Name string - - mu sync.Mutex - l *MemLazyDll - proc uintptr -} - -func (p *MemLazyProc) Addr() uintptr { - p.mustFind() - return p.proc -} -func (p *MemLazyProc) Call(a ...uintptr) (r1 uintptr, r2 uintptr, lastErr error) { - p.mustFind() - return syscall.SyscallN(p.Addr(), a...) -} -func (p *MemLazyProc) Find() error { - if atomic.LoadUintptr(&p.proc) == 0 { - p.mu.Lock() - defer p.mu.Unlock() - - if p.proc == 0 { - err := p.l.Load() - if err != nil { - return err - } - - proc, err := p.l.dll.ProcAddressByName(p.Name) - if err != nil { - return err - } - atomic.StoreUintptr(&p.proc, proc) - } - - } - return nil -} -func (p *MemLazyProc) mustFind() {} +package dll + +import ( + "sync" + "sync/atomic" + "syscall" + "unsafe" + + "github.com/pkg/errors" + "golang.zx2c4.com/wireguard/windows/driver/memmod" +) + +type MemLazyDll struct { + Data []byte + + mu sync.Mutex + dll *memmod.Module +} + +var _ LazyDll = (*MemLazyDll)(nil) + +func (d *MemLazyDll) Handle() uintptr { + d.mustLoad() + return d.dll.BaseAddr() +} +func (d *MemLazyDll) Load() (err error) { + if atomic.LoadPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll))) != nil { + return nil + } + d.mu.Lock() + defer d.mu.Unlock() + if d.dll != nil { + return nil + } + + dll, err := memmod.LoadLibrary(d.Data) + if err != nil { + return errors.WithStack(err) + } + atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&d.dll)), unsafe.Pointer(dll)) + return nil +} +func (d *MemLazyDll) mustLoad() { + err := d.Load() + if err != nil { + panic(err) + } +} + +func (d *MemLazyDll) NewProc(name string) LazyProc { + return &MemLazyProc{Name: name, l: d} +} + +type MemLazyProc struct { + Name string + + mu sync.Mutex + l *MemLazyDll + proc uintptr +} + +func (p *MemLazyProc) Addr() uintptr { + p.mustFind() + return p.proc +} +func (p *MemLazyProc) Call(a ...uintptr) (r1 uintptr, r2 uintptr, lastErr error) { + p.mustFind() + return syscall.SyscallN(p.Addr(), a...) +} +func (p *MemLazyProc) Find() error { + if atomic.LoadUintptr(&p.proc) == 0 { + p.mu.Lock() + defer p.mu.Unlock() + + if p.proc == 0 { + err := p.l.Load() + if err != nil { + return err + } + + proc, err := p.l.dll.ProcAddressByName(p.Name) + if err != nil { + return err + } + atomic.StoreUintptr(&p.proc, proc) + } + + } + return nil +} +func (p *MemLazyProc) mustFind() {} diff --git a/dll/sys.go b/dll/sys.go index a4a3fc9..92a8938 100644 --- a/dll/sys.go +++ b/dll/sys.go @@ -1,15 +1,15 @@ -package dll - -import ( - "golang.org/x/sys/windows" -) - -type SysLazyDll struct { - windows.LazyDLL -} - -var _ LazyDll = (*SysLazyDll)(nil) - -func (l *SysLazyDll) NewProc(name string) LazyProc { - return l.LazyDLL.NewProc(name) -} +package dll + +import ( + "golang.org/x/sys/windows" +) + +type SysLazyDll struct { + windows.LazyDLL +} + +var _ LazyDll = (*SysLazyDll)(nil) + +func (l *SysLazyDll) NewProc(name string) LazyProc { + return l.LazyDLL.NewProc(name) +}