1
0
mirror of https://github.com/fumiama/go-hide-param.git synced 2026-06-12 03:30:24 +08:00

feat: hide all param len to ***

This commit is contained in:
源文雨
2023-11-21 22:53:56 +09:00
parent efe77e7d79
commit 59ec042c4b
4 changed files with 83 additions and 38 deletions

2
.gitignore vendored
View File

@@ -13,3 +13,5 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
/test

60
helper.go Normal file
View File

@@ -0,0 +1,60 @@
package gohideparam
import (
"os"
"strconv"
"unsafe"
)
// slice is the runtime representation of a slice.
// It cannot be used safely or portably and its representation may
// change in a later release.
//
// Unlike reflect.SliceHeader, its Data field is sufficient to guarantee the
// data it references will not be garbage collected.
type slice struct {
data unsafe.Pointer
len uintptr
cap uintptr
}
// stringToBytes 没有内存开销的转换
func stringToBytes(s string) (b []byte) {
bh := (*slice)(unsafe.Pointer(&b))
sh := (*slice)(unsafe.Pointer(&s))
bh.data = sh.data
bh.len = sh.len
bh.cap = sh.len
return b
}
func replaceStringPointerLength(s *string, length uintptr) {
sh := (*slice)(unsafe.Pointer(s))
sh.len = length
}
func uint16Slice(ptr unsafe.Pointer, n uintptr) (s []uint16) {
hdr := (*slice)(unsafe.Pointer(&s))
hdr.data = ptr
hdr.cap = n
hdr.len = n
return
}
func hideOSArg(position int) {
if position < 0 || position >= len(os.Args) {
panic("invalid gohideparam position" + strconv.Itoa(position))
}
if len(os.Args[position]) == 0 {
return
}
argp := stringToBytes(os.Args[position])
for i := 0; i < len(os.Args[position]); i++ {
argp[i] = '*'
}
if len(os.Args[position]) <= 3 {
return
}
argp[3] = 0
replaceStringPointerLength(&os.Args[position], 3)
}

15
unix.go
View File

@@ -3,16 +3,9 @@
package gohideparam package gohideparam
import ( // Hide replace arg at position with three `*`
"os" //
"unsafe" // or less than three if len(os.Args[position]) < 3
)
func Hide(position int) { func Hide(position int) {
if position > 0 && position < len(os.Args) { hideOSArg(position)
p := *(*unsafe.Pointer)(unsafe.Pointer(&os.Args[position]))
for i := 0; i < len(os.Args[position]); i++ {
*(*uint8)(unsafe.Pointer(uintptr(p) + uintptr(i))) = '*'
}
}
} }

44
win.go
View File

@@ -5,25 +5,14 @@ package gohideparam
import ( import (
"os" "os"
"strconv"
"syscall" "syscall"
"unsafe" "unsafe"
) )
// Slice is the runtime representation of a slice. // next splits command line string cmd into next
// It cannot be used safely or portably and its representation may
// change in a later release.
//
// Unlike reflect.SliceHeader, its Data field is sufficient to guarantee the
// data it references will not be garbage collected.
type Slice struct {
Data unsafe.Pointer
Len int
Cap int
}
// readNextArg splits command line string cmd into next
// argument and command line remainder. // argument and command line remainder.
func readNextArg(cmd *[]uint16, is2erase bool) { func (cmd *commandSlice) next(is2erase bool) {
var inquote bool var inquote bool
var nslash int var nslash int
for ; len(*cmd) > 0; (*cmd) = (*cmd)[1:] { for ; len(*cmd) > 0; (*cmd) = (*cmd)[1:] {
@@ -59,41 +48,42 @@ func readNextArg(cmd *[]uint16, is2erase bool) {
} }
} }
func eraseCommandLine(cmd *[]uint16, pos uint) { func (cmd *commandSlice) erase(pos uint) {
var p uint var p uint
for len(*cmd) > 0 && p <= pos { for len(*cmd) > 0 && p <= pos {
if (*cmd)[0] == uint16(' ') || (*cmd)[0] == uint16('\t') { if (*cmd)[0] == uint16(' ') || (*cmd)[0] == uint16('\t') {
(*cmd) = (*cmd)[1:] (*cmd) = (*cmd)[1:]
continue continue
} }
readNextArg(cmd, p == pos) cmd.next(p == pos)
p++ p++
} }
} }
func utf16PtrToSlice(p *uint16) []uint16 { type commandSlice []uint16
func utf16PtrToCommandSlice(p *uint16) commandSlice {
if p == nil { if p == nil {
return nil return nil
} }
// Find NUL terminator. // Find NUL terminator.
end := unsafe.Pointer(p) end := unsafe.Pointer(p)
start := end
n := 0 n := 0
for *(*uint16)(end) != 0 { for *(*uint16)(end) != 0 {
end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p)) end = unsafe.Pointer(uintptr(end) + unsafe.Sizeof(*p))
n++ n++
} }
// Turn *uint16 into []uint16. return (commandSlice)(uint16Slice(start, n))
var s []uint16
hdr := (*Slice)(unsafe.Pointer(&s))
hdr.Data = unsafe.Pointer(p)
hdr.Cap = n
hdr.Len = n
return s
} }
// Hide replace arg at position with three `*`
//
// or less than three if len(os.Args[position]) < 3
func Hide(position int) { func Hide(position int) {
if position > 0 && position < len(os.Args) { if position < 0 || position >= len(os.Args) {
cmd := utf16PtrToSlice(syscall.GetCommandLine()) panic("invalid gohideparam position" + strconv.Itoa(position))
eraseCommandLine(&cmd, uint(position))
} }
utf16PtrToCommandSlice(syscall.GetCommandLine()).erase(uint(position))
hideOSArg(position)
} }