Code Monkey home page Code Monkey logo

Comments (9)

ddddddO avatar ddddddO commented on July 23, 2024 5

tweet見ました!pingu面白いですね🎉

自分の環境がWSL(Debian)なのですが、雑にやってみたことを共有します🙇
概要としては、pinger.SetPrivileged(true) + sudo setcap cap_net_raw=+ep path/to/pingu で、pingu実行できました、という内容です🙋‍♀️

  • 以下は、ipv4.ping_group_range の値をデフォルト値に戻して、pinguが失敗する状態に戻し、
16:06:32 > pingu github.com
PING github.com (13.114.40.48) type `Ctrl-C` to abort
 ...        .     ...   ..    ..     .........            seq=0 32bytes from 13.114.40.48: ttl=44 time=14.012181ms
 ...     ....          ..  ..      ... .....  .. ..       seq=1 32bytes from 13.114.40.48: ttl=44 time=14.069981ms
 ...    .......      ...         ... . ..... #######      seq=2 32bytes from 13.114.40.48: ttl=44 time=13.407582ms
.....  ........ .###############.....  ... ##########.  . seq=3 32bytes from 13.114.40.48: ttl=44 time=12.866183ms
 .... ........#####################.  ... ###########     seq=4 32bytes from 13.114.40.48: ttl=44 time=13.314583ms
      ....... ######################.... ############     seq=5 32bytes from 13.114.40.48: ttl=44 time=13.455982ms
.    .  .... ########################... ###########      seq=6 32bytes from 13.114.40.48: ttl=44 time=14.013181ms
   ..   ....#########################.. .###########      seq=7 32bytes from 13.114.40.48: ttl=44 time=13.393982ms
    .       #########################.   .##########      seq=8 32bytes from 13.114.40.48: ttl=44 time=13.824781ms
   ....     .########################.      ########      seq=9 32bytes from 13.114.40.48: ttl=44 time=14.165381ms
^C
───── github.com ping statistics ─────
PACKET STATISTICS: 10 transmitted => 10 received (0% loss)
ROUND TRIP: min=12.866183ms avg=13.652381ms max=14.165381ms stddev=403.038µs
~
16:06:45 > sysctl -n net.ipv4.ping_group_range
0       2147483647
~
16:09:47 >
~
16:09:57 > sudo sysctl -w net.ipv4.ping_group_range="1 0"
[sudo] ochi のパスワード:
net.ipv4.ping_group_range = 1 0
~
16:13:25 > pingu github.com
PING github.com (52.192.72.89) type `Ctrl-C` to abort
[ERROR] an error occurred when running ping: socket: permission denied
~
  • pinguのmain.go内を以下に変更し、
func initPinger(host string) (*ping.Pinger, error) {
	pinger, err := ping.NewPinger(host)
	if err != nil {
		return nil, fmt.Errorf("failed to init pinger %w", err)
	}

	// Listen for Ctrl-C.
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	go func() {
		for range c {
			pinger.Stop()
		}
	}()

	color.New(color.FgHiWhite, color.Bold).Printf(
		"PING %s (%s) type `Ctrl-C` to abort\n",
		pinger.Addr(),
		pinger.IPAddr(),
	)

	pinger.OnRecv = pingerOnrecv
	pinger.OnFinish = pingerOnFinish

	pinger.SetPrivileged(true)
	// ↑を追記し以下をコメントアウト
	// if runtime.GOOS == "windows" {
	// 	pinger.SetPrivileged(true)
	// }

	return pinger, nil
}
  • go buildして、setcapを実行してpinguが実行できることを確認しました~
16:34:35 > go build
~/github.com/ddddddO/pingu
16:35:05 > ls | grep pingu 
pingu
~/github.com/ddddddO/pingu
16:35:25 > pwd
/home/ochi/github.com/ddddddO/pingu
~/github.com/ddddddO/pingu
16:35:35 > sudo setcap cap_net_raw=+ep /home/ochi/github.com/ddddddO/pingu/pingu
~/github.com/ddddddO/pingu
16:35:40 > ./pingu github.com
PING github.com (52.192.72.89) type `Ctrl-C` to abort
 ...        .     ...   ..    ..     .........            seq=0 32bytes from 52.192.72.89: ttl=42 time=10.733279ms
 ...     ....          ..  ..      ... .....  .. ..       seq=1 32bytes from 52.192.72.89: ttl=42 time=11.776576ms
 ...    .......      ...         ... . ..... #######      seq=2 32bytes from 52.192.72.89: ttl=42 time=10.263379ms
.....  ........ .###############.....  ... ##########.  . seq=3 32bytes from 52.192.72.89: ttl=42 time=10.910478ms
 .... ........#####################.  ... ###########     seq=4 32bytes from 52.192.72.89: ttl=42 time=10.813078ms
      ....... ######################.... ############     seq=5 32bytes from 52.192.72.89: ttl=42 time=10.704279ms
.    .  .... ########################... ###########      seq=6 32bytes from 52.192.72.89: ttl=42 time=11.123278ms
   ..   ....#########################.. .###########      seq=7 32bytes from 52.192.72.89: ttl=42 time=11.486577ms
^C
───────── github.com ping statistics ─────────
PACKET STATISTICS: 8 transmitted => 8 received (0% loss)
ROUND TRIP: min=10.263379ms avg=10.976365ms max=11.776576ms stddev=446.473µs
~/github.com/ddddddO/pingu
16:36:16 > 

from pingu.

sheepla avatar sheepla commented on July 23, 2024 2

細かいところまで調査くださりありがとうございます!✨
とても勉強になります。

sysctl でシステムの設定を変えなくても、 pinger.SetPrivileged(true) + sudo setcap cap_net_raw=+ep path/to/pingu の方法で行けそうですね。この方針で実装してみたいと思います。

from pingu.

sheepla avatar sheepla commented on July 23, 2024 2

--privilege オプションを実装しました。

	if opts.Privilege || runtime.GOOS == "windows" {
		pinger.SetPrivileged(true)
	}

WSLでも pingu -P 127.0.0.1 のようにすればたぶん動くと思います。

from pingu.

ddddddO avatar ddddddO commented on July 23, 2024 1

こちらこそ勉強になりました🌞!

from pingu.

sheepla avatar sheepla commented on July 23, 2024

Already surpported in native Windows environment: #2
Thank you, mattn!

if runtime.GOOS == "windows"
    pinger.SetPrivileged(true)
}

However, not working in the WSL environment, so it seems that it is necessary to enable privileged mode.

from pingu.

sheepla avatar sheepla commented on July 23, 2024

Is there a way to determine from the Go code whether the current environment is native Linux or WSL?

from pingu.

sheepla avatar sheepla commented on July 23, 2024

Oops, I accidentally closed the issue.

from pingu.

sheepla avatar sheepla commented on July 23, 2024

I have confirmed that it works with WSL under the following conditions. Thank you, kurehajime!

https://twitter.com/kurehajime/status/1543073623392473089

  • --privilege: Enable
  • Execute sudo setcap cap_net_raw=+ep /path/to/pingu before run pingu

from pingu.

sheepla avatar sheepla commented on July 23, 2024

Thank you everyone for your cooperation! I will close this issue. ☑️

from pingu.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.