summary refs log tree commit diff
path: root/nixos/tests/v2ray.nix
blob: 4808e149d31ed1e4d47c96034858e687ffc10975 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import ./make-test-python.nix ({ lib, pkgs, ... }: let

  v2rayUser = {
    # A random UUID.
    id = "a6a46834-2150-45f8-8364-0f6f6ab32384";
    alterId = 0; # Non-zero support will be disabled in the future.
  };

  # 1080 [http proxy] -> 1081 [vmess] -> direct
  v2rayConfig = {
    inbounds = [
      {
        tag = "http_in";
        port = 1080;
        listen = "127.0.0.1";
        protocol = "http";
      }
      {
        tag = "vmess_in";
        port = 1081;
        listen = "127.0.0.1";
        protocol = "vmess";
        settings.clients = [v2rayUser];
      }
    ];
    outbounds = [
      {
        tag = "vmess_out";
        protocol = "vmess";
        settings.vnext = [{
          address = "127.0.0.1";
          port = 1081;
          users = [v2rayUser];
        }];
      }
      {
        tag = "direct";
        protocol = "freedom";
      }
    ];
    routing.rules = [
      {
        type = "field";
        inboundTag = "http_in";
        outboundTag = "vmess_out";
      }
      {
        type = "field";
        inboundTag = "vmess_in";
        outboundTag = "direct";
      }
    ];
  };

in {
  name = "v2ray";
  meta = with lib.maintainers; {
    maintainers = [ servalcatty ];
  };
  machine = { pkgs, ... }: {
    environment.systemPackages = [ pkgs.curl ];
    services.v2ray = {
      enable = true;
      config = v2rayConfig;
    };
    services.httpd = {
      enable = true;
      adminAddr = "foo@example.org";
    };
  };

  testScript = ''
    start_all()

    machine.wait_for_unit("httpd.service")
    machine.wait_for_unit("v2ray.service")
    machine.wait_for_open_port(80)
    machine.wait_for_open_port(1080)
    machine.succeed(
        "curl --fail --max-time 10 --proxy http://localhost:1080 http://localhost"
    )
  '';
})