Among the byproducts of the US invasion of Iraq in 2003 is the fact that the country has been turned into an Iranian protectorate. The Americans may come and go, but the Iranians – Iraq’s neighbours to the east – are there to stay.

As aNew York Times feature in July by Tim Arango demonstrates, Iranian imports flood Iraqi markets, Tehran-backed militias operate with impunity, and Iran dominates Iraqi politics through allied parliamentarians and militias. To Iran’s east lies Afghanistan, which may, with time, meet a fate similar to Iraq, which was made into an Iranian fiefdom due to short-sighted American policies.

Much has been made of Pakistan’s alleged enabling role in the Afghan Taliban insurgency. Pakistan is home to the Afghan Taliban leadership and its border regions have in the past served as safe havens for militants.The relationship between the Afghan Taliban and the Pakistani state is now more complicated. The Afghan Taliban insurgency is largely free from Pakistani control, financed by the drug trade and illicit mining in Afghanistan. It has the capacity to use anti-Pakistan jihadist networks that have emerged over the past decade to punish Islamabad. Adding to the Afghan Taliban’s independence is the fact that it has cultivated relations with its former foe, Iran, to counterbalance Pakistan.

In the wake of 9/11, Iran supported the US-led effort to overthrow the Afghan Talibanand replace it with a more moderate regime. During the 1990s, Tehran sponsored the anti-Taliban Northern Alliance and nearly invaded Afghanistan in 1998 after ten Iranian diplomats were killed in Afghan Taliban controlled territory.

But after 9/11, Iran also began hedging in Afghanistan by hosting senior Al Qaeda officials and members of Osama bin Laden’s family. Subsequently it began to offer tactical support to the emerging Afghan Taliban insurgency – a relationship that has only grown in recent years.

US and Afghan officials – including, most recently, the governor of the Farah province – allege that Tehran has provided funds, shelter, and weapons to the Afghan Taliban, aiding the group’s operations in both the west and south of the country. And the previous Afghan Taliban leader, Mullah Akhtar Mansour, was killed in a 2016 US drone strike in Pakistan after returning from Iran, where, according to the Wall Street Journal, his family resided. Since 2011, Afghan Taliban envoys have made repeated visited to Tehran.

The growing Iranian relationship with the Afghan Taliban is on top of additional channels of influence in the country. Iran has historically sponsored Afghan Shiite political groups. Like Pakistan, Iran has hosted millions of Afghan refugees who have fled violence in their homeland since 1979 and supported some mujahideen factions in the 1980s anti-Soviet jihad and the subsequent civil war.

The Iranian Revolutionary Guard Corps (IRGC) uses these networks to recruit Afghan Shiites to fight in Syria to prop up the Assad regime. The Afghan brigade in Syria, known as the Liwa Fatemiyoun, has more than 20,000 fighters. The IRGC uses some Afghans from the Fatemiyoun to train Houthi rebels in Yemen. There is risk that these forces could eventually be used in Afghanistan.

Iran can also leverage its militant proxies of the past. Today, some constituent forces of the Northern Alliance are rejoining to oppose the government of US-backed president Ashraf Ghani. Iran, like the United States, supported Ghani’s predecessor, Hamid Karzai, by providing him “bags of cash” to be disbursed among his supporters. This support may have enabled Karzai to take a tougher line with the United States, including most glaringly, by refusing to sign a long-term security deal with Washington.

The Afghan Taliban safe haven is within Afghanistan itself, especially after Pakistan’s military operations in North Waziristan in 2014, which largely purged foreign militants from the tribal areas bordering Afghanistan.

In 2016, Iran became Afghanistan’s largest trade partner, supplanting Pakistan. Afghanistan’s weak economy runs a trade deficit with all of its neighbors. But its trade with Iran is far more one-sided than its trade with Pakistan. At the same time, India is assisting landlocked Afghanistan in developing an alternative trade corridor through the Iranian port of Chabahar, which New Delhi has committed to develop as a counterweight to the Chinese-operated port of Gwadar in Pakistan.

In February, the commander of US forces in Afghanistan, Gen John Nicholson, described the Chabahar port as “beneficial” for Afghanistan, stating that it would offer Afghanistan an “alternative to going through Pakistan”. He also welcomed the trilateral trade accord between Afghanistan, India and Iran.

To be clear, the notion of Iranian influence in Afghanistan is not a problem in itself. Tehran is bound to be a player in its poor, landlocked neighbour. The issue is potential Iranian dominance. And by sidelining Pakistan – as some Trump administration officials seem to advocate – the US may be making Iran the predominant external force in the country within a decade.

A political settlement with the Afghan Taliban is the only way, other than a precipitous withdrawal, to end America’s longest war. To achieve one, the Afghan Taliban must be stalled on the battlefield until it opts for peace. A regional diplomatic process, including Iran, is also necessary.

But, above all, an enduring peace in Afghanistan is possible if and only if the United States uses its remaining years in the country to help Kabul and Islamabad develop the capacity and mechanisms to control their shared border and population flows along it. In deed, and, eventually, in word, Kabul must give up its claims to Pakistani territory and recognise its international border with Pakistan. A political settlement in Afghanistan, backed by regional states and anchored by Afghanistan-Pakistan cooperation, could release the region’s latent cross-border trade potential.

There is a closing window of opportunity for Washington to provide an enduring framework for peace in Afghanistan after its eventual withdrawal. In this, there is a role for GCC states, such as the UAE, which have a long record of providing assistance to Afghanistan and Pakistan, to support infrastructure development that bolsters Afghanistan’s trade with and via Pakistan.

A campaign by Washington to make Islamabad the odd man out, however, raises the risk of Tehran’s dominion extending further to the east.

Arif Rafiq (@arifcrafiq) is president of Vizier Consulting, LLC, a political risk advisory company, a fellow at the Center for Global Policy and a non-resident fellow at the Middle East Institute