Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GoナニモワカラナイけどChatGPTと共に解決したい課題が解けるかやってみた #fukuokago

akase244
October 24, 2024

GoナニモワカラナイけどChatGPTと共に解決したい課題が解けるかやってみた #fukuokago

「Fukuoka.go#20」でLT登壇した際のスライドです。

akase244

October 24, 2024
Tweet

More Decks by akase244

Other Decks in Programming

Transcript

  1. October 23, 2024. Fukuoka.go#20 • pingコマンド ◦ ICMP(Internet Control Message

    Protocol) ◦ 監視先のホスト名・アドレス ◦ 実行回数 ◦ RTT(Round Trip Time)
  2. October 23, 2024. Fukuoka.go#20 $ ping www.google.com -c 3 PING

    www.google.com (123.123.123.123) 56(84) bytes of data. 64 bytes from www.example.net (123.123.123.123): icmp_seq=1 ttl=116 time=20.6 ms 64 bytes from www.example.net (123.123.123.123): icmp_seq=2 ttl=116 time=21.4 ms 64 bytes from www.example.net (123.123.123.123): icmp_seq=3 ttl=116 time=24.5 ms --- www.google.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 20.604/22.165/24.490/1.675 ms 実行結果
  3. October 23, 2024. Fukuoka.go#20 $ sudo hping3 www.google.com -p 443

    -S -c 3 HPING www.google.com (XXXXXXXXXXXXXXX 123.123.123.123): S set, 40 headers + 0 data bytes len=46 ip=123.123.123.123 ttl=120 DF id=0 sport=443 flags=SA seq=0 win=65535 rtt=27.9 ms len=46 ip=123.123.123.123 ttl=120 DF id=0 sport=443 flags=SA seq=1 win=65535 rtt=25.7 ms len=46 ip=123.123.123.123 ttl=120 DF id=0 sport=443 flags=SA seq=2 win=65535 rtt=19.5 ms --- www.google.com hping statistic --- 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max = 19.5/24.4/27.9 ms 実行結果
  4. October 23, 2024. Fukuoka.go#20 % sudo nping www.google.com --tcp -p

    443 --flags syn -c 3 Starting Nping 0.7.95 ( https://nmap.org/nping ) at 2024-10-09 22:33 JST SENT (0.0120s) TCP 192.168.0.126:2941 > 142.250.206.228:443 S ttl=64 id=32827 iplen=40 seq=1858308333 win=1480 RCVD (0.0325s) TCP 142.250.206.228:443 > 192.168.0.126:2941 SA ttl=120 id=0 iplen=44 seq=2406887558 win=65535 <mss 1412> SENT (1.0122s) TCP 192.168.0.126:2941 > 142.250.206.228:443 S ttl=64 id=32827 iplen=40 seq=1858308333 win=1480 RCVD (1.0313s) TCP 142.250.206.228:443 > 192.168.0.126:2941 SA ttl=120 id=0 iplen=44 seq=2422489814 win=65535 <mss 1412> SENT (2.0124s) TCP 192.168.0.126:2941 > 142.250.206.228:443 S ttl=64 id=32827 iplen=40 seq=1858308333 win=1480 RCVD (2.0405s) TCP 142.250.206.228:443 > 192.168.0.126:2941 SA ttl=120 id=0 iplen=44 seq=2438261292 win=65535 <mss 1412> Max rtt: 27.952ms | Min rtt: 19.055ms | Avg rtt: 22.476ms Raw packets sent: 3 (120B) | Rcvd: 3 (138B) | Lost: 0 (0.00%) Nping done: 1 IP address pinged in 2.04 seconds 実行結果
  5. October 23, 2024. Fukuoka.go#20 $ sudo hping3 監視対象ホスト -p 80

    -S -c 5 HPING 監視対象ホスト (eth0 123.123.123.123): S set, 40 headers + 0 data bytes len=46 ip=123.123.123.123 ttl=51 DF id=0 sport=80 flags=SA seq=0 win=64240 rtt=19.3 ms len=46 ip=123.123.123.123 ttl=51 DF id=0 sport=80 flags=SA seq=1 win=64240 rtt=19.6 ms len=46 ip=123.123.123.123 ttl=51 DF id=0 sport=80 flags=SA seq=2 win=64240 rtt=19.3 ms len=52 ip=123.123.123.123 ttl=51 DF id=0 sport=80 flags=SA seq=0 win=64240 rtt=0.0 ms len=46 ip=123.123.123.123 ttl=51 DF id=29975 sport=80 flags=A seq=0 win=251 rtt=0.0 ms --- 監視対象ホスト hping statistic --- 3 packets transmitted, 5 packets received, -66% packet loss round-trip min/avg/max = 19.3/19.4/19.6 ms hping3の実行結果
  6. October 23, 2024. Fukuoka.go#20 $ sudo hping3 監視対象ホスト -p 80

    -S -c 5 HPING 監視対象ホスト (eth0 123.123.123.123): S set, 40 headers + 0 data bytes len=46 ip=123.123.123.123 ttl=51 DF id=0 sport=80 flags=SA seq=0 win=64240 rtt=19.3 ms len=46 ip=123.123.123.123 ttl=51 DF id=0 sport=80 flags=SA seq=1 win=64240 rtt=19.6 ms len=46 ip=123.123.123.123 ttl=51 DF id=0 sport=80 flags=SA seq=2 win=64240 rtt=19.3 ms len=52 ip=123.123.123.123 ttl=51 DF id=0 sport=80 flags=SA seq=0 win=64240 rtt=0.0 ms len=46 ip=123.123.123.123 ttl=51 DF id=29975 sport=80 flags=A seq=0 win=251 rtt=0.0 ms --- 監視対象ホスト hping statistic --- 3 packets transmitted, 5 packets received, -66% packet loss round-trip min/avg/max = 19.3/19.4/19.6 ms hping3の実行結果
  7. October 23, 2024. Fukuoka.go#20 for i := 0; i <

    *count; i++ { start := time.Now() conn, err := net.Dial("tcp", *host+":"+*port) if err != nil { fmt.Println("Error:", err) continue } defer conn.Close() rtt := time.Since(start) fmt.Printf("RTT: %v\n", rtt) rttTimes = append(rttTimes, rtt) net.Dialを利用するパターン
  8. October 23, 2024. Fukuoka.go#20 for i := 0; i <

    count; i++ { start := time.Now() conn, err := net.DialTimeout("tcp", address, 2*time.Second) rtt := time.Since(start) if err != nil { fmt.Printf("Request %d: Connection failed: %s\n", i+1, err) } else { fmt.Printf("Request %d: Connected, RTT = %v\n", i+1, rtt) rttTimes = append(rttTimes, rtt) conn.Close() } net.DialTimeoutを利用するパターン
  9. October 23, 2024. Fukuoka.go#20 net.Dialのパターンの実行結果 $ go run sample_dial.go -host

    tsunagi.me -port 80 -c 2 RTT: 51.351408ms RTT: 34.728687ms Min RTT: 34.728687ms, Max RTT: 51.351408ms, Avg RTT: 43.040047ms
  10. $ go run sample_dial_timeout.go tsunagi.me 80 2 PING tsunagi.me:80 (tsunagi.me:80)

    Request 1: Connected, RTT = 56.609141ms Request 2: Connected, RTT = 32.416619ms --- tsunagi.me:80 statistics --- 2 packets transmitted, 2 packets received RTT min/avg/max = 32.416619ms/44.51288ms/56.609141ms October 23, 2024. Fukuoka.go#20 net.DialTimeoutの実行結果
  11. October 23, 2024. Fukuoka.go#20 packetSource := gopacket.NewPacketSource(handle, handle.LinkType()) for packet

    := range packetSource.Packets() { // 応答をチェック if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil { tcp := tcpLayer.(*layers.TCP) if tcp.SYN && tcp.ACK { duration := time.Since(start) fmt.Printf("Received SYN-ACK from %s:%d in %v\n", targetIP, targetPort, duration) return duration, nil } } } gopacketを使っていてかなりそれっぽい?
  12. October 23, 2024. Fukuoka.go#20 $ sudo go run sample_dial_timeout.go tsunagi.me

    80 # github.com/google/gopacket/pcap /root/go/pkg/mod/github.com/google/[email protected]/pcap/pcap_unix.go:34:10: fatal error: pcap.h: No such file or directory 34 | #include <pcap.h> | ^~~~~~~~ compilation terminated. なんか怒られた
  13. October 23, 2024. Fukuoka.go#20 $ sudo go run sample_dial_timeout.go tsunagi.me

    80 # command-line-arguments ./main.go:33:2: declared and not used: tcpHeader ./main.go:41:8: assignment mismatch: 1 variable but conn.WriteTo returns 2 values ./main.go:41:21: multiple-value ipHeader.Marshal() (value of type ([]byte, error)) in single-value context ./main.go:79:23: cannot use port (variable of type string) as int value in argument to sendSYN またなんか怒られた
  14. October 23, 2024. Fukuoka.go#20 • hping3のようにホスト、ポート番号、実行回数を引数に指定す る。 • SYNフラグを送信して、SYN/ACKを受信したら成功とする。 •

    実行時にシーケンス番号、ACK番号を確認して、送信したパ ケットの戻りパケットであることを保証したい。 • 1回実行する毎にRTT(Round Trip Time)を表示する。 • 実行が完了したらRTTのmin/max/avgを表示する。