mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
This commit extends the documentation effort by adding detailed explanatory comments to key CLI components and core functionality throughout the cmd/ directory. The changes focus on explaining WHY certain logic is needed, not just WHAT the code does, improving code maintainability and helping developers understand complex business decisions. Key improvements: - Main entry points: Document CLI initialization, logging setup, and cache configuration with reasoning for design decisions - DNS proxy core: Explain DNS proxy constants, data structures, and core processing pipeline for handling DNS queries - Service management: Document service command structure, configuration patterns, and platform-specific service handling - Logging infrastructure: Explain log buffer management, level encoders, and log formatting decisions for different use cases - Metrics and monitoring: Document Prometheus metrics structure, HTTP endpoints, and conditional metric collection for performance - Network handling: Explain Linux-specific network interface filtering, virtual interface detection, and DNS configuration management - Hostname validation: Document RFC1123 compliance and DNS naming standards for system compatibility - Mobile integration: Explain HTTP retry logic, fallback mechanisms, and mobile platform integration patterns - Connection management: Document connection wrapper design to prevent log pollution during process lifecycle Technical details: - Added explanatory comments to 11 additional files in cmd/cli/ - Maintained consistent documentation style and format - Preserved all existing functionality while improving code clarity - Enhanced understanding of complex business logic and platform-specific behavior These comments help future developers understand the reasoning behind complex decisions, making the codebase more maintainable and reducing the risk of incorrect modifications during maintenance.
139 lines
2.8 KiB
Go
139 lines
2.8 KiB
Go
// Copied from https://github.com/secur30nly/go-self-delete
|
|
// with modification to suitable for ctrld usage.
|
|
|
|
/*
|
|
License: MIT Licence
|
|
|
|
References:
|
|
- https://github.com/LloydLabs/delete-self-poc
|
|
- https://twitter.com/jonasLyk/status/1350401461985955840
|
|
*/
|
|
|
|
package cli
|
|
|
|
import (
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
var supportedSelfDelete = false
|
|
|
|
type FILE_RENAME_INFO struct {
|
|
Union struct {
|
|
ReplaceIfExists bool
|
|
Flags uint32
|
|
}
|
|
RootDirectory windows.Handle
|
|
FileNameLength uint32
|
|
FileName [1]uint16
|
|
}
|
|
|
|
type FILE_DISPOSITION_INFO struct {
|
|
DeleteFile bool
|
|
}
|
|
|
|
// dsOpenHandle opens a handle to the specified file with DELETE access
|
|
func dsOpenHandle(pwPath *uint16) (windows.Handle, error) {
|
|
handle, err := windows.CreateFile(
|
|
pwPath,
|
|
windows.DELETE,
|
|
0,
|
|
nil,
|
|
windows.OPEN_EXISTING,
|
|
windows.FILE_ATTRIBUTE_NORMAL,
|
|
0,
|
|
)
|
|
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
return handle, nil
|
|
}
|
|
|
|
// dsRenameHandle renames a file handle to a stream name
|
|
func dsRenameHandle(hHandle windows.Handle) error {
|
|
var fRename FILE_RENAME_INFO
|
|
DS_STREAM_RENAME, err := windows.UTF16FromString(":deadbeef")
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
lpwStream := &DS_STREAM_RENAME[0]
|
|
fRename.FileNameLength = uint32(unsafe.Sizeof(lpwStream))
|
|
|
|
windows.NewLazyDLL("kernel32.dll").NewProc("RtlCopyMemory").Call(
|
|
uintptr(unsafe.Pointer(&fRename.FileName[0])),
|
|
uintptr(unsafe.Pointer(lpwStream)),
|
|
unsafe.Sizeof(lpwStream),
|
|
)
|
|
|
|
err = windows.SetFileInformationByHandle(
|
|
hHandle,
|
|
windows.FileRenameInfo,
|
|
(*byte)(unsafe.Pointer(&fRename)),
|
|
uint32(unsafe.Sizeof(fRename)+unsafe.Sizeof(lpwStream)),
|
|
)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// dsDepositeHandle marks a file handle for deletion
|
|
func dsDepositeHandle(hHandle windows.Handle) error {
|
|
var fDelete FILE_DISPOSITION_INFO
|
|
fDelete.DeleteFile = true
|
|
|
|
err := windows.SetFileInformationByHandle(
|
|
hHandle,
|
|
windows.FileDispositionInfo,
|
|
(*byte)(unsafe.Pointer(&fDelete)),
|
|
uint32(unsafe.Sizeof(fDelete)),
|
|
)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// selfDeleteExe performs self-deletion on Windows platforms
|
|
func selfDeleteExe() error {
|
|
var wcPath [windows.MAX_PATH + 1]uint16
|
|
var hCurrent windows.Handle
|
|
|
|
_, err := windows.GetModuleFileName(0, &wcPath[0], windows.MAX_PATH)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
hCurrent, err = dsOpenHandle(&wcPath[0])
|
|
if err != nil || hCurrent == windows.InvalidHandle {
|
|
return err
|
|
}
|
|
|
|
if err := dsRenameHandle(hCurrent); err != nil {
|
|
_ = windows.CloseHandle(hCurrent)
|
|
return err
|
|
}
|
|
_ = windows.CloseHandle(hCurrent)
|
|
|
|
hCurrent, err = dsOpenHandle(&wcPath[0])
|
|
if err != nil || hCurrent == windows.InvalidHandle {
|
|
return err
|
|
}
|
|
|
|
if err := dsDepositeHandle(hCurrent); err != nil {
|
|
_ = windows.CloseHandle(hCurrent)
|
|
return err
|
|
}
|
|
|
|
return windows.CloseHandle(hCurrent)
|
|
}
|