SOSInternet is a .NET console application designed to monitor the internet connection and automatically reboot a TP-Link router when the connection drops. The application is particularly useful in situations involving large downloads over extended periods, which can cause the router to freeze.
The application was designed following SOLID principles and utilizes the Dependency Injection pattern to ensure a modular, testable, and easily extensible structure.
SOSInternet/
├── src/
│ ├── SOSInternet/
│ │ ├── Program.cs # Application entry point
│ │ ├── appsettings.json # Configuration file
│ │ ├── Core/
│ │ │ ├── Interfaces/ # Interfaces for services
│ │ │ ├── Models/ # Data models
│ │ │ └── Services/ # Service implementations
│ │ ├── Infrastructure/
│ │ │ ├── Configuration/ # Configuration management
│ │ │ └── Logging/ # Logger configuration
│ │ └── Utilities/
│ │ ├── Extensions/ # Service extensions
│ │ └── Playwright/ # Playwright Setup
├── docs/
│ ├── README.md # Main documentation
└── .gitignore # .gitignore file for GitHub
The Core namespace contains the application's business logic:
-
Interfaces: Defines the contracts for the services
IInternetChecker: Interface for checking the internet connectionIRouterRebooter: Interface for rebooting the routerIConnectionMonitor: Interface for monitoring the connection
-
Models: Defines the data models
ConnectionStatus: Model for the connection statusRouterSettings: Model for the router settingsConnectionSettings: Model for the connection settings
-
Services: Implements the business logic
InternetChecker: Implementation for checking the connectionTpLinkRouterRebooter: Implementation for rebooting the TP-Link routerConnectionMonitor: Implementation for monitoring the connection
The Infrastructure namespace manages the application's infrastructure:
-
Configuration: Manages the application's configurations
AppSettings: Class for configuration management
-
Logging: Configures and manages logging
LoggerConfiguration: Class for logger configuration
The Utilities namespace contains utilities and extensions:
- Extensions: Extensions to simplify service configuration
ServiceCollectionExtensions: Extensions for service registration
- The application is started by
Program.cs - Configurations are loaded from
appsettings.json - The logger is configured
- Services are registered via Dependency Injection
- The
ConnectionMonitoris started, which:- Periodically checks the internet connection via
InternetChecker - If the connection is lost, it attempts to restore it
- After a configurable number of failed attempts, it reboots the router via
TpLinkRouterRebooter - Continues monitoring after the reboot
- Periodically checks the internet connection via
The application's configurations are managed through the appsettings.json file:
{
"RouterSettings": {
"Url": "http://192.168.1.1",
"Username": "admin",
"Password": "password",
"UseEncryption": false
},
"ConnectionSettings": {
"CheckIntervalSeconds": 15,
"RetryAttemptsBeforeReboot": 4,
"WaitAfterRebootSeconds": 120,
"PingTargets": [
"8.8.8.8",
"1.1.1.1",
"google.com"
]
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
},
"FileLogging": {
"Enabled": true,
"Path": "logs",
"RetentionDays": 30
}
}
}Url: Router administration URLUsername: Username for router access (if necessary)Password: Password for router accessUseEncryption: Indicates if the password is encrypted
CheckIntervalSeconds: Interval in seconds between connection checksRetryAttemptsBeforeReboot: Number of attempts before rebooting the routerWaitAfterRebootSeconds: Waiting time in seconds after router rebootPingTargets: List of addresses to ping to verify the connection
LogLevel: Logging levels for different categoriesFileLogging: Configuration for file logging
The application implements the following security measures:
- Sensitive credentials can be encrypted in the configuration file
- A secure decryption mechanism is implemented
- No credentials are hardcoded in the source code
The application uses NLog for structured logging:
- Logs are written to both console and file
- Log files are archived daily
- Logs are retained for a configurable period (default: 30 days)
- Console logs are colored based on the logging level
The architecture is designed to be easily extensible:
- Support for different router types via the
IRouterRebooterinterface - Support for different connection checking methods via the
IInternetCheckerinterface - Ability to add new features without modifying existing code
- .NET 8.0 or higher
- Administrative access to the TP-Link router
- Working network connection
- Microsoft.Extensions.Hosting
- Microsoft.Extensions.DependencyInjection
- Microsoft.Extensions.Configuration
- Microsoft.Extensions.Logging
- Microsoft.Playwright
- NLog.Extensions.Logging
- Clone the repository
- Configure the
appsettings.jsonfile with your router settings - Build the application with
dotnet build - Run the application with
dotnet run
The application is designed to run in the background and does not require user interaction. It can be configured for automatic startup on system boot using Windows Task Scheduler or a system service.
Application logs are available in the logs directory and can be used to diagnose any issues.
-
Application cannot access the router
- Verify that the router URL is correct
- Verify that the login credentials are correct
- Verify that the router is reachable from the network
-
Application does not detect connection loss
- Verify that the ping targets are reachable
- Increase the number of attempts before reboot
-
Application fails to reboot the router
- Verify that the login credentials are correct
- Verify that the router supports rebooting via the web interface
- Verify that the router's web interface has not changed
Contributions are welcome! To contribute to the project:
- Fork the repository
- Create a branch for your feature (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is distributed under the MIT License.