# Network Issues Guide

Diagnose and resolve networking problems.

## Connectivity Issues

Test connectivity between services:

```bash
#!/usr/bin/env nu

def test-service-connectivity [source-service target-service port] {
  print $"Testing connectivity from ($source-service) to ($target-service):($port)"

  let result = (provisioning network test-connectivity \
    --from $source-service \
    --to $target-service \
    --port $port)

  if $result.status == "connected" {
    print "✓ Connected"
  } else {
    print "✗ Connection failed"
    print $"  Error: ($result.error)"
  }
}
```

## DNS Issues

Diagnose DNS problems:

```bash
#!/usr/bin/env nu

def diagnose-dns [] {
  # Check DNS resolution
  let records = (provisioning dns list)
  $records | each { | record |
    print $"($record.name) → ($record.value)"
  }

  # Test DNS resolution
  let resolution = (provisioning network dns-test example.com)
  if $resolution.status == "resolved" {
    print "✓ DNS resolves correctly"
  } else {
    print "✗ DNS resolution failed"
  }
}
```

## Firewall Rules

Review and fix firewall rules:

```bash
#!/usr/bin/env nu

def check-firewall-rules [] {
  let rules = (provisioning security list-rules)

  print "Current firewall rules:"
  $rules | each { | rule |
    print $"  ($rule.direction) ($rule.protocol) ($rule.port_range): ($rule.source)"
  }

  # Test specific rules
  let test = (provisioning network test-firewall \
    --destination 10.0.1.0/24 \
    --port 443 \
    --protocol tcp)

  if $test.allowed {
    print "✓ Traffic allowed"
  } else {
    print "✗ Traffic blocked by firewall"
  }
}
```

## VPN Issues

Debug VPN connectivity:

```bash
#!/usr/bin/env nu

def diagnose-vpn [] {
  # Check VPN status
  let status = (provisioning vpn status)
  print $"VPN Status: ($status.status)"

  if $status.status != "connected" {
    # Try to reconnect
    provisioning vpn reconnect
    sleep 10sec

    # Check again
    let new_status = (provisioning vpn status)
    print $"After reconnect: ($new_status.status)"
  }

  # Test latency through VPN
  let latency = (provisioning network test-latency --target 10.255.0.1)
  print $"VPN latency: ($latency)ms"
}
```

## Load Balancer Issues

Resolve load balancer problems:

```bash
#!/usr/bin/env nu

def check-load-balancer [] {
  # Get backend health
  let backends = (provisioning load-balancer backends)

  backends | each { | backend |
    let health = $backend.health
    let status = if $health == "healthy" { "✓" } else { "✗" }
    print $"($status) ($backend.name): ($backend.status)"
  }

  # Check health check configuration
  let health_config = (provisioning load-balancer health-check-config)
  print $"Health check: ($health_config.path) every ($health_config.interval)s"
}
```

## Related Documentation

- [Troubleshooting Overview](./README.md)
- [Common Issues](./common-issues.md)
- [Networking Configuration](../infrastructure/providers.md)
