LoginSignup
0
0

More than 3 years have passed since last update.

【GoネットワークプログラムでIPv6対応させる】IPv6対応httpサービスの作り方

Last updated at Posted at 2020-08-06

理由

Goで書かれたネットワークプログラムをIPv6に対応させるためにはどうすればいいのかコード例をメモしておく

参考コード

main.go
package main
import (
    "log"
    "net/http"
)
func main() {
    log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/var/www/html"))))
}
/var/www/html/index.html
<html>
 <head>
  <title>test</title>
 </head>
 <body>
 <h1>This is ipv6 http server test.</h1>
 </body>
</html>

解説

Goのhttp.ListenAndServeの場合、何も意識する必要はありません

  • サーバのIPv6アドレスは
$ ip address show dev eth0 
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 9001 qdisc fq state UP group default qlen 1000
    link/ether 06:0a:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.92/25 brd 192.168.100.127 scope global dynamic eth0
       valid_lft 3558sec preferred_lft 3558sec
    inet 192.168.100.106/25 brd 192.168.100.127 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 2406:da14:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx/128 scope global dynamic 
       valid_lft 393sec preferred_lft 93sec
    inet6 fe80::40a:eaff:xxxx:xxxx/64 scope link 
       valid_lft forever preferred_lft forever
  • runする
$ go run main.go  
  • 確認
$ netstat -an | grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN    
  • アクセスしてみる
$ curl -i 'http://[2406:da14:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx]:8080/' 
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 112
Content-Type: text/html; charset=utf-8
Last-Modified: Thu, 06 Aug 2020 12:17:04 GMT
Date: Thu, 06 Aug 2020 12:22:11 GMT

<html>
 <head>
  <title>test</title>
 </head>
 <body>
 <h1>This is ipv6 http server test.</h1>
 </body>
</html>

まとめ

サーバにIPv6アドレスつけて、フィルタ/ファイアーウォール/セキュリティグループ等開けてあげれば、なにもしなくていいよ。
参考)AWS VPCでIPv6を使う

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0