#
# Copyright (c) 2020 Bubble, Inc.  All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
#
- name: Allow all from local
  iptables:
    chain: INPUT
    in_interface: lo
    jump: ACCEPT
    comment: Allow all from local
  become: yes

- name: Allow related and established connections
  iptables:
    chain: INPUT
    ctstate: ESTABLISHED,RELATED
    jump: ACCEPT
    comment: Allow related and established connections
  become: yes

- name: Allow SSH on ports 22 and 1202
  iptables:
    chain: INPUT
    protocol: tcp
    destination_port: "{{ item }}"
    ctstate: NEW
    syn: match
    jump: ACCEPT
    comment: Accept new SSH connections
  with_items:
    - 22
    - 1202
  become: yes
  when: fw_enable_ssh

- name: "Allow HTTP on port {{ item }}"
  iptables:
    chain: INPUT
    protocol: tcp
    destination_port: "{{ item }}"
    ctstate: NEW
    syn: match
    jump: ACCEPT
    comment: "Accept new HTTP ({{ item }}) connections"
  with_items:
    - 80
    - 1080
  become: yes
  when: fw_enable_http

- name: "Allow HTTPS on port {{ item }}"
  iptables:
    chain: INPUT
    protocol: tcp
    destination_port: "{{ item }}"
    ctstate: NEW
    syn: match
    jump: ACCEPT
    comment: "Accept new HTTPS ({{ item }}) connections"
  with_items:
    - 443
    - 1443
  become: yes
  when: fw_enable_http

- name: Drop everything else
  iptables:
    chain: INPUT
    policy: DROP
  become: yes

- name: save iptables v4 rules
  shell: iptables-save > /etc/iptables/rules.v4
  become: yes

- name: save iptables v6 rules
  shell: ip6tables-save > /etc/iptables/rules.v6
  become: yes
