Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1333eb85bb | |||
| dae84c0a85 | |||
|
ed85eb7616
|
|||
|
d8c0d86ca0
|
|||
|
bbd85885df
|
@@ -0,0 +1,28 @@
|
|||||||
|
// +build darwin
|
||||||
|
|
||||||
|
package staros
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// DefaultFreq - frequency, in Hz, middle A
|
||||||
|
DefaultFreq = 0.0
|
||||||
|
// DefaultDuration - duration in milliseconds
|
||||||
|
DefaultDuration = 0
|
||||||
|
)
|
||||||
|
|
||||||
|
// Beep beeps the PC speaker (https://en.wikipedia.org/wiki/PC_speaker).
|
||||||
|
func Beep(freq float64, duration int) error {
|
||||||
|
osa, err := exec.LookPath("osascript")
|
||||||
|
if err != nil {
|
||||||
|
// Output the only beep we can
|
||||||
|
_, err = os.Stdout.Write([]byte{7})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command(osa, "-e", `beep`)
|
||||||
|
return cmd.Run()
|
||||||
|
}
|
||||||
+138
@@ -0,0 +1,138 @@
|
|||||||
|
// +build linux
|
||||||
|
|
||||||
|
package staros
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Constants
|
||||||
|
const (
|
||||||
|
// This number represents the fixed frequency of the original PC XT's timer chip, which is approximately 1.193 MHz. This number
|
||||||
|
// is divided with the desired frequency to obtain a counter value, that is subsequently fed into the timer chip, tied to the PC speaker.
|
||||||
|
clockTickRate = 1193180
|
||||||
|
|
||||||
|
// linux/kd.h, start sound generation (0 for off)
|
||||||
|
kiocsound = 0x4B2F
|
||||||
|
|
||||||
|
// linux/input-event-codes.h
|
||||||
|
evSnd = 0x12 // Event type
|
||||||
|
sndTone = 0x02 // Sound
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// DefaultFreq - frequency, in Hz, middle A
|
||||||
|
DefaultFreq = 440.0
|
||||||
|
// DefaultDuration - duration in milliseconds
|
||||||
|
DefaultDuration = 200
|
||||||
|
)
|
||||||
|
|
||||||
|
// inputEvent represents linux/input.h event structure.
|
||||||
|
type inputEvent struct {
|
||||||
|
Time syscall.Timeval // time in seconds since epoch at which event occurred
|
||||||
|
Type uint16 // event type
|
||||||
|
Code uint16 // event code related to the event type
|
||||||
|
Value int32 // event value related to the event type
|
||||||
|
}
|
||||||
|
|
||||||
|
// ioctl system call manipulates the underlying device parameters of special files.
|
||||||
|
func ioctl(fd, name, data uintptr) error {
|
||||||
|
_, _, e := syscall.Syscall(syscall.SYS_IOCTL, fd, name, data)
|
||||||
|
if e != 0 {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Beep beeps the PC speaker (https://en.wikipedia.org/wiki/PC_speaker).
|
||||||
|
//
|
||||||
|
// On Linux it needs permission to access `/dev/tty0` or `/dev/input/by-path/platform-pcspkr-event-spkr` files for writing,
|
||||||
|
// and `pcspkr` module must be loaded. User must be in correct groups, usually `input` and/or `tty`.
|
||||||
|
//
|
||||||
|
// If it can not open device files, it will fallback to sending Bell character (https://en.wikipedia.org/wiki/Bell_character).
|
||||||
|
// For bell character in X11 terminals you can enable bell with `xset b on`. For console check `setterm` and `--blength` or `--bfreq` options.
|
||||||
|
//
|
||||||
|
// On macOS this just sends bell character. Enable `Audible bell` in Terminal --> Preferences --> Settings --> Advanced.
|
||||||
|
//
|
||||||
|
// On Windows it uses Beep function via syscall.
|
||||||
|
//
|
||||||
|
// On Web it plays hard coded beep sound.
|
||||||
|
func Beep(freq float64, duration int) error {
|
||||||
|
if freq == 0 {
|
||||||
|
freq = DefaultFreq
|
||||||
|
} else if freq > 20000 {
|
||||||
|
freq = 20000
|
||||||
|
} else if freq < 0 {
|
||||||
|
freq = DefaultFreq
|
||||||
|
}
|
||||||
|
|
||||||
|
if duration == 0 {
|
||||||
|
duration = DefaultDuration
|
||||||
|
}
|
||||||
|
|
||||||
|
period := int(float64(clockTickRate) / freq)
|
||||||
|
|
||||||
|
var evdev bool
|
||||||
|
|
||||||
|
f, err := os.OpenFile("/dev/tty0", os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
e := err
|
||||||
|
f, err = os.OpenFile("/dev/input/by-path/platform-pcspkr-event-spkr", os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
e = errors.New("beeep: " + e.Error() + "; " + err.Error())
|
||||||
|
|
||||||
|
// Output the only beep we can
|
||||||
|
_, err = os.Stdout.Write([]byte{7})
|
||||||
|
if err != nil {
|
||||||
|
return errors.New(e.Error() + "; " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
evdev = true
|
||||||
|
}
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
if evdev { // Use Linux evdev API
|
||||||
|
ev := inputEvent{}
|
||||||
|
ev.Type = evSnd
|
||||||
|
ev.Code = sndTone
|
||||||
|
ev.Value = int32(freq)
|
||||||
|
|
||||||
|
d := *(*[unsafe.Sizeof(ev)]byte)(unsafe.Pointer(&ev))
|
||||||
|
|
||||||
|
// Start beep
|
||||||
|
f.Write(d[:])
|
||||||
|
|
||||||
|
time.Sleep(time.Duration(duration) * time.Millisecond)
|
||||||
|
|
||||||
|
ev.Value = 0
|
||||||
|
d = *(*[unsafe.Sizeof(ev)]byte)(unsafe.Pointer(&ev))
|
||||||
|
|
||||||
|
// Stop beep
|
||||||
|
f.Write(d[:])
|
||||||
|
} else { // Use ioctl
|
||||||
|
// Start beep
|
||||||
|
err = ioctl(f.Fd(), kiocsound, uintptr(period))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Duration(duration) * time.Millisecond)
|
||||||
|
|
||||||
|
// Stop beep
|
||||||
|
err = ioctl(f.Fd(), kiocsound, uintptr(0))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package staros
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
rat float64 = 1.059463094 //2^(1/12)
|
||||||
|
C float64 = 493.8833013 * rat
|
||||||
|
CU = C * rat * rat
|
||||||
|
D = CU * rat
|
||||||
|
DU = D * rat
|
||||||
|
E = DU * rat
|
||||||
|
F = E * rat
|
||||||
|
FU = F * rat
|
||||||
|
G = FU * rat
|
||||||
|
GU = G * rat
|
||||||
|
A = GU * rat
|
||||||
|
AU = A * rat
|
||||||
|
B = AU * rat
|
||||||
|
)
|
||||||
|
|
||||||
|
func beepMusic(qual ...float64) {
|
||||||
|
for _, v := range qual {
|
||||||
|
fmt.Println(v)
|
||||||
|
Beep(v, 700)
|
||||||
|
time.Sleep(time.Millisecond * 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Music(t *testing.T) {
|
||||||
|
beepMusic(G, D, A, AU, A, G, F, D, DU, D, C, D, AU/2, C, G/2, C, D)
|
||||||
|
time.Sleep(time.Second * 3)
|
||||||
|
beepMusic(D,AU,A,G,A,D*2,F*2,G*2,F*2,D*2,D*2,C*2,D*2,DU*2,D*2,AU,A,E,G,FU)
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
// +build windows
|
||||||
|
|
||||||
|
package staros
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// DefaultFreq - frequency, in Hz, middle A
|
||||||
|
DefaultFreq = 587.0
|
||||||
|
// DefaultDuration - duration in milliseconds
|
||||||
|
DefaultDuration = 500
|
||||||
|
)
|
||||||
|
|
||||||
|
// Beep beeps the PC speaker (https://en.wikipedia.org/wiki/PC_speaker).
|
||||||
|
func Beep(freq float64, duration int) error {
|
||||||
|
if freq == 0 {
|
||||||
|
freq = DefaultFreq
|
||||||
|
} else if freq > 32767 {
|
||||||
|
freq = 32767
|
||||||
|
} else if freq < 37 {
|
||||||
|
freq = DefaultFreq
|
||||||
|
}
|
||||||
|
|
||||||
|
if duration == 0 {
|
||||||
|
duration = DefaultDuration
|
||||||
|
}
|
||||||
|
|
||||||
|
kernel32, _ := syscall.LoadLibrary("kernel32.dll")
|
||||||
|
beep32, _ := syscall.GetProcAddress(kernel32, "Beep")
|
||||||
|
|
||||||
|
defer syscall.FreeLibrary(kernel32)
|
||||||
|
|
||||||
|
_, _, e := syscall.Syscall(uintptr(beep32), uintptr(2), uintptr(int(freq)), uintptr(duration), 0)
|
||||||
|
if e != 0 {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -3,11 +3,81 @@
|
|||||||
package staros
|
package staros
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"b612.me/stario"
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type FileLock struct {
|
||||||
|
fd int
|
||||||
|
filepath string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FileLock) openFileForLock() error {
|
||||||
|
fd, err := syscall.Open(f.filepath, syscall.O_CREAT|syscall.O_RDONLY, 0600)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f.filepath = f.filepath
|
||||||
|
f.fd = fd
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FileLock) Lock(Exclusive bool) error {
|
||||||
|
var lockType int
|
||||||
|
if Exclusive {
|
||||||
|
lockType = syscall.LOCK_EX
|
||||||
|
} else {
|
||||||
|
lockType = syscall.LOCK_SH
|
||||||
|
}
|
||||||
|
if err := f.openFileForLock(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return syscall.Flock(f.fd, lockType)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FileLock) LockNoBlocking(Exclusive bool) error {
|
||||||
|
var lockType int
|
||||||
|
if Exclusive {
|
||||||
|
lockType = syscall.LOCK_EX
|
||||||
|
} else {
|
||||||
|
lockType = syscall.LOCK_SH
|
||||||
|
}
|
||||||
|
if err := f.openFileForLock(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err := syscall.Flock(f.fd, lockType|syscall.LOCK_NB)
|
||||||
|
if err != nil {
|
||||||
|
syscall.Close(f.fd)
|
||||||
|
if err == syscall.EWOULDBLOCK {
|
||||||
|
return ERR_ALREADY_LOCKED
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FileLock) Unlock() error {
|
||||||
|
err := syscall.Flock(f.fd, syscall.LOCK_UN)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return syscall.Close(f.fd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *FileLock) LockWithTimeout(tm time.Duration, Exclusive bool) error {
|
||||||
|
return stario.WaitUntilTimeout(tm, func(tmout chan struct{}) error {
|
||||||
|
err := f.Lock(Exclusive)
|
||||||
|
select {
|
||||||
|
case <-tmout:
|
||||||
|
f.Unlock()
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func timespecToTime(ts syscall.Timespec) time.Time {
|
func timespecToTime(ts syscall.Timespec) time.Time {
|
||||||
return time.Unix(int64(ts.Sec), int64(ts.Nsec))
|
return time.Unix(int64(ts.Sec), int64(ts.Nsec))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
module b612.me/staros
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
|
require (
|
||||||
|
b612.me/stario v0.0.7
|
||||||
|
b612.me/win32api v0.0.1
|
||||||
|
b612.me/wincmd v0.0.2
|
||||||
|
golang.org/x/sys v0.4.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
|
||||||
|
)
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
b612.me/stario v0.0.7 h1:QbQcsHCVLE6vRgVrPN4+9DGiSaC6IWdtm4ClL2tpMUg=
|
||||||
|
b612.me/stario v0.0.7/go.mod h1:or4ssWcxQSjMeu+hRKEgtp0X517b3zdlEOAms8Qscvw=
|
||||||
|
b612.me/win32api v0.0.1 h1:vLFB1xhO6pd9+zB2EyaapKB459Urv3v+C1YwgwOFEWo=
|
||||||
|
b612.me/win32api v0.0.1/go.mod h1:MHu0JBQjzxQ2yxpZPUBbn5un45o67eF5iWKa4Q9e0yE=
|
||||||
|
b612.me/wincmd v0.0.2 h1:Ub1WtelVT6a3vD4B6zDYo3UPO/t9ymnI3x1dQPJcrGw=
|
||||||
|
b612.me/wincmd v0.0.2/go.mod h1:bwpyCKfSDY8scSMo3Lrd0Qnqvpz7/CILL7oodfG0wgo=
|
||||||
|
golang.org/x/crypto v0.0.0-20220313003712-b769efc7c000/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||||
|
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 h1:S25/rfnfsMVgORT4/J61MJ7rdyseOZOyvLIrZEZ7s6s=
|
||||||
|
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||||
|
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
|
||||||
|
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
|
||||||
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
+16
-9
@@ -1,3 +1,4 @@
|
|||||||
|
//go:build !windows
|
||||||
// +build !windows
|
// +build !windows
|
||||||
|
|
||||||
package staros
|
package staros
|
||||||
@@ -67,7 +68,13 @@ func NetSpeeds(duration time.Duration) ([]NetSpeed, error) {
|
|||||||
for k, v := range list1 {
|
for k, v := range list1 {
|
||||||
recv := float64(list2[k].RecvBytes-v.RecvBytes) / duration.Seconds()
|
recv := float64(list2[k].RecvBytes-v.RecvBytes) / duration.Seconds()
|
||||||
send := float64(list2[k].SendBytes-v.SendBytes) / duration.Seconds()
|
send := float64(list2[k].SendBytes-v.SendBytes) / duration.Seconds()
|
||||||
res = append(res, NetSpeed{v.Name, recv, send})
|
res = append(res, NetSpeed{
|
||||||
|
Name: v.Name,
|
||||||
|
RecvSpeeds: recv,
|
||||||
|
SendSpeeds: send,
|
||||||
|
RecvBytes: list2[k].RecvBytes,
|
||||||
|
SendBytes: list2[k].SendBytes,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@@ -87,12 +94,12 @@ func NetSpeedsByName(duration time.Duration, name string) (NetSpeed, error) {
|
|||||||
|
|
||||||
// NetConnections return all TCP/UDP/UNIX DOMAIN SOCKET Connections
|
// NetConnections return all TCP/UDP/UNIX DOMAIN SOCKET Connections
|
||||||
// if your uid != 0 ,and analysePid==true ,you should have CAP_SYS_PRTACE and CAP_DAC_OVERRIDE/CAP_DAC_READ_SEARCH Caps
|
// if your uid != 0 ,and analysePid==true ,you should have CAP_SYS_PRTACE and CAP_DAC_OVERRIDE/CAP_DAC_READ_SEARCH Caps
|
||||||
func NetConnections(analysePid bool,types string) ([]NetConn, error) {
|
func NetConnections(analysePid bool, types string) ([]NetConn, error) {
|
||||||
var result []NetConn
|
var result []NetConn
|
||||||
var inodeMap map[string]int64
|
var inodeMap map[string]int64
|
||||||
var err error
|
var err error
|
||||||
var fileList []string
|
var fileList []string
|
||||||
if types=="" || strings.Contains(strings.ToLower(types),"all") {
|
if types == "" || strings.Contains(strings.ToLower(types), "all") {
|
||||||
fileList = []string{
|
fileList = []string{
|
||||||
"/proc/net/tcp",
|
"/proc/net/tcp",
|
||||||
"/proc/net/tcp6",
|
"/proc/net/tcp6",
|
||||||
@@ -101,14 +108,14 @@ func NetConnections(analysePid bool,types string) ([]NetConn, error) {
|
|||||||
"/proc/net/unix",
|
"/proc/net/unix",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if strings.Contains(strings.ToLower(types),"tcp") {
|
if strings.Contains(strings.ToLower(types), "tcp") {
|
||||||
fileList =append(fileList,"/proc/net/tcp","/proc/net/tcp6")
|
fileList = append(fileList, "/proc/net/tcp", "/proc/net/tcp6")
|
||||||
}
|
}
|
||||||
if strings.Contains(strings.ToLower(types),"udp") {
|
if strings.Contains(strings.ToLower(types), "udp") {
|
||||||
fileList =append(fileList,"/proc/net/udp","/proc/net/udp6")
|
fileList = append(fileList, "/proc/net/udp", "/proc/net/udp6")
|
||||||
}
|
}
|
||||||
if strings.Contains(strings.ToLower(types),"unix") {
|
if strings.Contains(strings.ToLower(types), "unix") {
|
||||||
fileList =append(fileList,"/proc/net/unix")
|
fileList = append(fileList, "/proc/net/unix")
|
||||||
}
|
}
|
||||||
if analysePid {
|
if analysePid {
|
||||||
inodeMap, err = GetInodeMap()
|
inodeMap, err = GetInodeMap()
|
||||||
|
|||||||
Reference in New Issue
Block a user