Skip to content

Commit

Permalink
Merge pull request #1 from joel0/v2
Browse files Browse the repository at this point in the history
V2.0.1
  • Loading branch information
joel0 authored Dec 3, 2017
2 parents fc58cb2 + 96000c6 commit dd0bc90
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 147 deletions.
12 changes: 9 additions & 3 deletions HostnamePlus/Controllers/IndexController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using HostnamePlus.Models;

namespace HostnamePlus.Controllers
{
/// <summary>
/// This controller chooses the view based on the UserAgent.
/// Curl gets a simplified text-only response, for readability.
/// </summary>
public class IndexController : Controller
{
public IActionResult Index()
Expand All @@ -17,6 +18,11 @@ public IActionResult Index()
view = "Curl";
Response.ContentType = "text/plain";
}

Response.Headers.Add("Link",
String.Format(
"<{0}>; rel=preload; as=script,<{1}>; rel=preload; as=style",
Program.getOtherIpJsPath, Program.mainCssPath));
return View(view, model);
}
}
Expand Down
17 changes: 10 additions & 7 deletions HostnamePlus/Controllers/OtherIpController.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using HostnamePlus.Models;

Expand All @@ -13,11 +9,18 @@ namespace HostnamePlus.Controllers
public class OtherIpController : Controller
{
// GET: api/OtherIp
/// <summary>
/// This API returns a JSON object with the request's IP and hostname.
/// It is intended to be loaded from IPv4 if the main page was loaded
/// via IPv6, and vice versa.
/// </summary>
/// <returns>The client's connection inforation.</returns>
[HttpGet]
public IpModel Get()
public IndexModel Get()
{
Response.Headers.Add("Access-Control-Allow-Origin", "*");
return new IpModel(Request);
String origin = String.Format("{0} {1}.{0} {2}.{0}", Program.BASE_URL, "ipv4", "ipv6");
Response.Headers.Add("Access-Control-Allow-Origin", origin);
return new IndexModel(Request);
}
}
}
1 change: 1 addition & 0 deletions HostnamePlus/HostnamePlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BuildBundlerMinifier" Version="2.6.362" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
</ItemGroup>
Expand Down
54 changes: 44 additions & 10 deletions HostnamePlus/Models/IndexModel.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,76 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace HostnamePlus.Models
{
/// <summary>
/// A model with the information for the home page. This model injests a
/// HttpRequest to populate the fields.
/// </summary>
public class IndexModel
{
private const String BASE_URL = "hostname.jmay.us";
private HttpRequest Request;
/// <summary>
/// The request object of connection from the client.
/// </summary>
private readonly HttpRequest Request;

/// <summary>
/// Constructs the model with the client's request connection.
/// </summary>
/// <param name="Request">The HttpRequest that the client opened to
/// load the page</param>
public IndexModel(HttpRequest Request)
{
this.Request = Request;
}

/// <summary>
/// The URL for the API to load the other IP from on the client side.
/// If the client is connected via IPv4, this URL is for the IPv6 only
/// API.
/// </summary>
public String OtherIpAPIURL {
get {
// If the connection is IPv6, hit the IPv4 API, and vice versa.
String subdomain = IsIPv6 ? "ipv4" : "ipv6";
return String.Format("//{0}.{1}/api/OtherIp", subdomain, BASE_URL);
return String.Format("//{0}.{1}/api/OtherIp",
subdomain, Program.BASE_URL);
}
}

/// <summary>
/// The friendly text for the alternate IP family.
/// </summary>
public String OtherIpType {
get {
return IsIPv6 ? "IPv4" : "IPv6";
}
}

private IPAddress RemoteIpAddress {
/// <summary>
/// Simple check of the IP address family.
/// </summary>
public bool IsIPv6 {
get {
return Request.HttpContext.Connection.RemoteIpAddress;
return RemoteIpAddress.AddressFamily == AddressFamily.InterNetworkV6;
}
}

public bool IsIPv6 {
/// <summary>
/// The client's IP address, sourced from the Request.
/// </summary>
private IPAddress RemoteIpAddress {
get {
return RemoteIpAddress.AddressFamily == AddressFamily.InterNetworkV6;
return Request.HttpContext.Connection.RemoteIpAddress;
}
}

/// <summary>
/// The reverse DNS of the client's IP, or N/A if the DNS lookup fails.
/// IPv6 reverse DNS lookups often fail.
/// </summary>
public String HostName {
get {
try {
Expand All @@ -54,12 +81,19 @@ public String HostName {
}
}

/// <summary>
/// A string of the client's IP.
/// </summary>
public String IP {
get {
return RemoteIpAddress.ToString();
}
}

/// <summary>
/// A string of the client's User Agent from the request headers. Empty
/// string if the header is missing.
/// </summary>
public String UserAgent {
get {
return Request.Headers["User-Agent"].ToString();
Expand Down
33 changes: 0 additions & 33 deletions HostnamePlus/Models/IpModel.cs

This file was deleted.

89 changes: 18 additions & 71 deletions HostnamePlus/Pages/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,12 @@
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>Your Hostname</title>

<style type="text/css">
body {
background-color: black;
color: white;
font-size: 0.75em;
font-family: "Century Gothic",CenturyGothic,AppleGothic,sans-serif;
text-align: center;
}
.main {
background-color: #003a4f;
margin-left: auto;
margin-right: auto;
margin-top: 20px;
margin-bottom: 10px;
max-width: 700px;
width: auto;
padding: 10px 20px 20px 20px;
text-align: left;
}
.versionLabel {
font-size: 0.75em;
color: #7f9ca7;
}
h1, h2, h3, h4, h5, h6 {
font-family: "Century Gothic",CenturyGothic,AppleGothic,sans-serif;
font-variant: small-caps;
font-weight: 400;
margin-bottom: 5px;
margin-top: 40px;
}
h1 {
margin-top: 0px;
text-align: right;
}
h3 {
margin-top: 0px;
margin-bottom: 0px;
margin-left: 15px;
font-size: medium;
}
hr {
border: none;
height: 1px;
background-color: red;
}
p, ul {
margin-left: 30px;
margin-top: 5px;
font-size: small;
line-height: 1.5;
}
ul {
margin-left: initial;
}
a {
color: lightcyan;
}
a:hover {
text-decoration: none;
}
</style>
<environment include="Development">
<link rel="stylesheet" href="~/css/main.css" />
</environment>
<environment exclude="Development">
<link rel="stylesheet" href="~/css/main.min.css" />
</environment>
</head>
<body>
<div class="main">
Expand All @@ -86,5 +21,17 @@
&copy;2015-2017 Joel May

@RenderSection("Scripts", required: false)
<environment include="Development">
@RenderSection("DevelopmentScripts", required: false)
@{
IgnoreSection("ProductionScripts");
}
</environment>
<environment exclude="Development">
@RenderSection("ProductionScripts", required: false)
@{
IgnoreSection("DevelopmentScripts");
}
</environment>
</body>
</html>
10 changes: 4 additions & 6 deletions HostnamePlus/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace HostnamePlus
{
public class Program
{
public const String BASE_URL = "hostname.jmay.us";
public static String getOtherIpJsPath;
public static String mainCssPath;

public static void Main(string[] args)
{
BuildWebHost(args).Run();
Expand Down
27 changes: 15 additions & 12 deletions HostnamePlus/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.HttpOverrides;

namespace HostnamePlus
{
Expand All @@ -27,23 +24,29 @@ public void ConfigureServices(IServiceCollection services)
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
Program.getOtherIpJsPath = "/js/GetOtherIp.js";
Program.mainCssPath = "/css/main.css";
} else {
app.UseExceptionHandler("/Error");
Program.getOtherIpJsPath = "/js/GetOtherIp.min.js";
Program.mainCssPath = "/css/main.min.css";
}

// Note: this is for being reverse proxied. Remove if using Kesteral without a reverse proxy.
// The reverse proxy server must set the XForwardedFor header.
app.UseForwardedHeaders(new ForwardedHeadersOptions {
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});

app.UseStaticFiles();

app.UseMvc(routes =>
{
app.UseMvc(routes => {
routes.MapRoute(
name: "default",
template: "{controller=Index}/{action=Index}/{id?}");
template: "{controller=Index}/{action=Index}/");
});
}
}
Expand Down
Loading

0 comments on commit dd0bc90

Please sign in to comment.