#
# Copyright (c) 2020 Bubble, Inc.  All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
#
- name: Set the cert name
  shell: set_cert_name.sh /home/mitmproxy/mitmproxy {{ cert_name }} "{{ cert_org }}" {{ cert_cn }}

- name: Reuse bubble mitm certs if available
  shell: reuse_bubble_mitm_certs.sh

- name: Copy bubble_config.py to /home/mitmproxy/mitmproxy
  template:
    src: bubble_config.py.j2
    dest: /home/mitmproxy/mitmproxy/bubble_config.py
    owner: mitmproxy
    group: mitmproxy
    mode: 0500

- name: Ensure mitmproxy user owns all mitmproxy files
  shell: chown -R mitmproxy /home/mitmproxy/mitmproxy

- name: Install supervisor conf file
  copy:
    src: supervisor_mitmproxy.conf
    dest: /etc/supervisor/conf.d/mitmproxy.conf
    owner: root
    group: root
    mode: 0400

- name: Install mitmdump_monitor supervisor conf file
  copy:
    src: supervisor_mitmdump_monitor.conf
    dest: /etc/supervisor/conf.d/mitmdump_monitor.conf

- name: "Allow MITM private port"
  iptables:
    chain: INPUT
    action: insert
    rule_num: 7
    protocol: tcp
    destination_port: 8888
    ctstate: NEW
    syn: match
    jump: ACCEPT
    comment: Accept new local connections on mitm port
  become: yes

- name: reload supervisord
  shell: supervisorctl reload
